default
import std::core::default; · source
Types
Default
type trait Default {
static default() -> This;
}
Default supplies a canonical zero value so generic code can construct without knowing the concrete type: zero for numbers, false for boolean, '�' for char. The containers implement it as "a fresh empty one that allocates nothing", so an Array<T> or HashMap<K, V> can sit in a struct that derives its own default.
Constructors
static default() -> This;
Implementors
implement trait Default for i8
implement trait Default for i16
implement trait Default for i32
implement trait Default for i64
implement trait Default for u8
implement trait Default for u16
implement trait Default for u32
implement trait Default for u64
implement trait Default for i128
implement trait Default for u128
implement trait Default for usize
implement trait Default for isize
implement trait Default for f32
implement trait Default for f64
implement trait Default for boolean
implement trait Default for char
implement<T> trait Default for struct Array<T, GlobalAlloc>
std::collections::array
implement<K, V> trait Default for struct HashMap<K, V, GlobalAlloc>
std::collections::hashmap
implement<T> trait Default for struct HashSet<T, GlobalAlloc>
std::collections::hashset
implement trait Default for struct String<GlobalAlloc>
std::collections::string
type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type trait Name : Base {
method(&this, other: &This) -> Ret;
}Names a set of methods a type may implement. Inside the body This is the implementing type; methods may carry default bodies, and a trait may require a base trait (type trait Ord : Eq). Generic code asks for a trait with where T: Name.
type trait Default {
static default() -> This;
}Default: a canonical "zero value" for a type.
static name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
static Default::default() -> ThisThisThe implementing type, inside a trait or implement body. other: &This in a trait method is a reference to whichever type implements it; This::Item names an associated type.
import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
type trait Default {
static default() -> This;
}Default: a canonical "zero value" for a type.
std::core::defaultfalseThe boolean literal. Booleans are a distinct 1-byte type, not interchangeable with integers; if and while conditions must be boolean.
type struct Array<T, A = GlobalAlloc> where A: Allocator {
ptr: T*;
length: i64;
capacity: i64;
alloc: A;
}Array<T, A>: growable, heap-backed, contiguous sequence.
std::collections::arraytype struct HashMap<K, V, A = GlobalAlloc> where A: Allocator {
buckets: Entry<K, V>**;
bucket_count: u64;
length: u64;
alloc: A;
seed: u64;
}HashMap<K, V, A>: key-value map with amortized O(1) operations.
std::collections::hashmapstatic Default::default() -> Thisimplement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
implement trait Trait for Type { ... }Introduces a trait implementation: the block supplies, for one concrete type, the method bodies the trait requires.
language referenceimplement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language referenceprimitive usizePointer-width unsigned integer; the natural type for sizes and indices.
primitive typetype parameter Ttype struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct GlobalAlloc {}Zero-sized handle to the process-wide allocator. Construct with GlobalAlloc::new(); all instances dispatch through the same underlying backend.
type parameter Ktype parameter Vtype struct HashSet<T, A = GlobalAlloc> {
map: HashMap<T, (), A>;
}HashSet<T, A>: unordered collection of unique values.
std::collections::hashsettype struct String<A = GlobalAlloc> {
buffer: RawBuffer<u8, A>;
length: u64;
}String: owned, heap-allocated, length-typed UTF-8.
std::collections::string