Rust’s standard library

    As with its C++ namesake, everything can be referenced through a namespace prefix or via a import.

    Some of std is implicitly available by a special that is automatically used (along with a reference to the std crate) without declaration. The prelude contains functionality that virtually all code is likely to use and therefore Rust spares code from having to import it:

    • Iterators traits of various kinds - Iterator, Exten, IntoIterator etc.
    • Result<> and Option<> enums
    • Conversion traits AsRef, AsMut, Into, From
    • Vec heap allocated vector
    • Other traits such as Drop, Fn, FnMut, FnOnce, Box, Clone, Copy, Send, Sized, Sync, PartialEq, PartialOrd etc.
    • Macros such as println!, format!, assert! etc.

    There are various sub-modules under std that concern themselves with aspects of development. Here are just some of them:

    1. clone – the Clone trait
    2. collections - contains the standard collection types for sequences, maps, sets, and miscellaneous. e.g. Vec and HashMap are members of this module.
    3. env – environmental helpers - command line arguments, status codes, environment variables, temporary folder
    4. fmt – utilities for formatting and printing strings
    5. fs - filesystem manipulation
    6. io – Read and Write traits that are implemented by streams / buffers in file system and networking, stdio functionality
    7. mem – memory primitives
    8. net – networking
    9. process – spawn, fork, exec etc.

    Note that Rust’s std namespace contains a lot of stuff not in the standard C or C++ libraries and a lot of things are not directly analogous.
    For example the standard C / C++ library have very little to say about sockets, or path manipulation, or atomically incrementing numbers, or creating threads.

    Note that because due to the decimal point being used on a float, you have to prefix f32 or f64 to literals when you call them so the compiler can figure out what you’re doing.

    Standard Traits

    Some traits are system defined and in some cases can be derived automatically.

    The Drop trait allows you do something when an object is dropped, such as add additional logging or whatever.

    A struct implementing a Copy trait can be copied through assignment, i.e. if you assign a to b then a and b now how copies of the object, independent of each other.
    The Copy trait really only useful when you have small amounts of data that represent a type or value of some kind.
    TODO copy example, e.g. struct PlayingCard { suit: Suit, rank: Rank }
    If you find yourself with a type that is larger, or contains heap allocated memory then you should use clone.

    A struct implementing the Clone trait has a .clone() method. Unlike Copy you must explicitly .clone() the instance to create another.
    TODO clone example

    TODO ordering