Skip to content

Latest commit

 

History

History
75 lines (58 loc) · 4.64 KB

play-std.org

File metadata and controls

75 lines (58 loc) · 4.64 KB

Rust’s Standard Libraries’s Tour

The Standard Library ‘s source module is std/lib.rs which includes

First The Rust Standard Library divided into a number of focused modules.

For example

  • the ~option~ and ~result~ modules define optional and error-handling types, Option<T> and Result<T, E>;
  • the ~iter~ module defines Rust’s iterator trait, Iterator, which works with the for loop [9] to access collections.

Second, The Rust Standard Library documents implicit methods on primitive types [8] (implicit because primitives are implemented by the compiler) and exports many modules with same name as primitive types.

For example

  • there is a page std/primitive.i32.html for primitive type i32,
  • and there is a page std/i32/index.html for the module std::i32. Note: as of std 1.76.0 (07dca489a 2024-02-04) modules like std::i32 are marked as “Deprecation planned” because “Redundant constants module for the ~i32~ primitive type.”

Third, The Standard Library defines module ~prelude~, The Rust Prelude, a small collection of items - mostly traits - that are imported into every module of every crate.

Fourth finally, The Standard Library exports a number of standard macros.

Containers and collections

The Standard Library exposes three common ways to deal with contiguous regions of memory:

  • Vec<T> - A heap-allocated vector that is resizable at runtime.
  • [T; n] - A inline array with a fixed size at compile time.
  • [T] - A dynamically sized slice into any other kind of contiguous storage, whether heap-allocated or not. Slices can only be handled through some kind of pointer, and as such come in many flavors such as:
    • &[T] - shared slice
    • &mut [T] - mutable slice
    • Box<T> - owned slice

The Standard Library defines many methods for primitive type str, a UTF-8 string slice. Rust str is typically accessed as immutable reference: &str. Use the owned String for building and mutating strings. For converting to strings use the format! macro, and for converting from strings use the FromStr trait.

Data may be shared in a single-threaded setting by placing it in a reference-counted (‘Rc’) box, the Rc<T> struct. If the data is further contained in a Cell or RefCell, it may be mutated as well. In a concurrent stetting data may be shared by placing it in a atomically-reference-counted (‘Arc’) box, the Arc<T> struct, with a Mutex to get the same effect.

The collections module defines maps like HashMap, sets like HashSet, sequences like LinkedLists and other typical collection types.

Platform abstractions and I/O

The Standard Library is largely concerned with abstracting over differences in common platforms, most notably Windows and Unix derivatives.

Common types of I/O are defined in the modules io, fs, and net.

The thread module contains Rust’s threading abstractions. The sync module contains further primitve shared memory types and channel types for message passing.