Crate std_prelude
source ·Expand description
use std_prelude::* so you can be ready to code!
Traits
Not having common traits imported is one of the most annoying gotchas in rust when coming from
other languages. When you are used to the language, you expect the commonly used methods to
always work… not so in rust! Using Vec::from_iter is extremely common, but you must first
import the FromIterator trait.
The following are the traits that are exported and why:
- std::ascii::AsciiExt: adds the
to_ascii_uppercaseonto&strtypes. - std::borrow::Borrow: for manually defining the
Borrowtrait. - std::cmp::{Ord, PartialOrd}: for manually defining the Ordering traits and using them in trait bounds.
- std::fmt::Debug: allows you to define Debug manually and use in trait bounds.
- std::hash::{Hash, Hasher}: allows you to define Hash manually and use in trait bounds.
- std::fmt::Write as FmtWrite: adds
write_stronto byte buffers (such asString). RenamedFmtWriteto avoid conflict withstd::io::Write - std::io::BufRead: the
BufReadtrait allows you to use the methods associated with theBufReaderstruct (also imported). - std::io::Read: allows you to use
file.read() - std::io::Seek: allows you to use
file.seek() - std::io::Write as IoWrite: allows you to use
file.write()andfile.write_all(). RenamedIoWriteto avoid conflict withstd::fmt::Write - std::ops::{Deref, DerefMut}: allows deref
through
*vand also enables Deref coercions - std::str::FromStr: allows you to use
type::from_strconstructor for several types. This is what is implicitly called withstr::parse<_>()
Structs
These are extremely commonly used types and it is annoying to have to reimport them all the time.
-
std::borrow::Cow: A clone-on-write smart pointer (often called copy on write in other languages). This is used by many libraries to be as efficient as possible when returned data is identical to the data passed in, among other uses.
-
std::collections::{BTreeMap, HashMap, HashSet}: ordered-dict, dict and set
-
std::cmp::Ordering: the enum type used in the Ordering traits.
-
std::ffi::OsString: os agnostic (non unicode) string type.
std::path::PathBufuses this. -
std::fs::File: for opening files.
File::opento open a file for readingFile::writeto open a file for writing
-
std::fs::OpenOptions: file opening options.
-
std::fs::ReadDir: to iterate over the entries in a directory.
-
std::io::BufReader: the BufRead struct wraps
io::Readusing a buffer reducing the number of OS calls and giving helpful methodsread_line(): read a single linelines(): return an iterator over all lines.split(byte: u8): return an iterator which splits at the chosen byte.
-
std::io::BufWriter: similar to
BufReader, buffers writes to reduce the number of calls to the OS. Provides no new methods. -
std::rc::Rc: reference counted pointer
-
std::sync::Arc: atomically reference counted pointer
-
std::sync::Mutex: mutual exclusion primitive for threading.
-
std::sync::atomic::{AtomicBool, AtomicIsize, AtomicUsize}: basic atomic types. Good for unique ids and lots of other use cases.
-
std::sync::atomic::Ordering as AtomicOrdering: necessary for performing operations on atomic types. For incrementing a counter use
val.fetch_add(1, AtomicOrdering::SeqCst). Renamed to not conflict withstd::cmp::Ordering. -
std::sync::atomic::ATOMIC_USIZE_INIT: initialized
AtomicUsizeof 0. Use withstatic COUNTER: AtomicUsize = ATOMIC_USIZE_INIT; -
std::time::Duration: an amount of time, used for
std::thread::sleep.
Functions
These are mostly just “nice to have” functions and it is really unlikely that they would ever be overriden.
- std::cmp::{max, min}: get the max or min of two comparable integers.
- std::mem::{size_of, size_of_val}: get the size of a type. This is safe and common enough that it should be always available.
- std::thread::sleep: put the thread to sleep for a
Duration. - std::thread::spawn: spawn a function in a new thread. In rust this is memory safe, so it is nice to have it always available.
Modules (primitive type only)
The following modules are imported so that it is easy to access their relevant constants and constructors.
- u8 u16 u64 usize: unsigned integer modules with
MAXandMIN - i8 i16 i64 isize: signed integer modules with
MAXandMIN - f32 f64: floating point modules with not just
MAXandMINbut alsoNAN,INFINITY, etc as well as a f32::consts and f64::consts modules with basic mathematical constants likePIandE. - str: core string type with
from_utf8function.
Modules
- Constants for the
f32single-precision floating point type. - Constants for the
f64double-precision floating point type. - i8Deprecation plannedRedundant constants module for the
i8primitive type. - i16Deprecation plannedRedundant constants module for the
i16primitive type. - i64Deprecation plannedRedundant constants module for the
i64primitive type. - isizeDeprecation plannedRedundant constants module for the
isizeprimitive type. - Utilities for the
strprimitive type. - u8Deprecation plannedRedundant constants module for the
u8primitive type. - u16Deprecation plannedRedundant constants module for the
i16primitive type. - u64Deprecation plannedRedundant constants module for the
u64primitive type. - usizeDeprecation plannedRedundant constants module for the
usizeprimitive type.
Macros
- Derive macro generating an impl of the trait
Debug. - Derive macro generating an impl of the trait
Hash. - Derive macro generating an impl of the trait
Ord. - Derive macro generating an impl of the trait
PartialOrd.
Structs
- A thread-safe reference-counting pointer. ‘Arc’ stands for ‘Atomically Reference Counted’.
- A boolean type which can be safely shared between threads.
- An integer type which can be safely shared between threads.
- An integer type which can be safely shared between threads.
- An ordered map based on a B-Tree.
- The
BufReader<R>struct adds buffering to any reader. - Wraps a writer and buffers its output.
- A
Durationtype to represent a span of time, typically used for system timeouts. - An object providing access to an open file on the filesystem.
- A hash map implemented with quadratic probing and SIMD lookup.
- A mutual exclusion primitive useful for protecting shared data
- Options and flags which can be used to configure how a file is opened.
- A type that can represent owned, mutable platform-native strings, but is cheaply inter-convertible with Rust strings.
- A slice of a path (akin to
str). - An owned, mutable path (akin to
String). - A single-threaded reference-counting pointer. ‘Rc’ stands for ‘Reference Counted’.
- Iterator over the entries in a directory.
Enums
- Atomic memory orderings
- A clone-on-write smart pointer.
- An
Orderingis the result of a comparison between two values. - Enumeration of possible methods to seek within an I/O object.
Constants
- ATOMIC_USIZE_INITDeprecatedAn atomic integer initialized to
0.
Traits
- AsciiExtDeprecatedExtension methods for ASCII-subset only operations.
- A trait for borrowing data.
- A
BufReadis a type ofReader which has an internal buffer, allowing it to perform extra ways of reading. ?formatting.- Used for immutable dereferencing operations, like
*v. - Used for mutable dereferencing operations, like in
*v = 1;. - A trait for writing or formatting into Unicode-accepting buffers or streams.
- Conversion from an
Iterator. - Parse a value from a string
- A hashable type.
- A trait for hashing an arbitrary stream of bytes.
- A trait for objects which are byte-oriented sinks.
- Trait for types that form a total order.
- Trait for types that form a partial order.
- The
Readtrait allows for reading bytes from a source. - The
Seektrait provides a cursor which can be moved within a stream of bytes.
Functions
- Returns the size of a type in bytes.
- Returns the size of the pointed-to value in bytes.
- Puts the current thread to sleep for at least the specified amount of time.
- Spawns a new thread, returning a
JoinHandlefor it.