core
core is the bottom of the library. It neither allocates nor performs I/O, and everything above it is built from what lives here. Much of the language is defined against these declarations: a..b builds a Range, for-in drives an Iterator, a < b on a user type calls Ord::compare, and [T] iteration hands you a Slice<T>.
public module core::intrinsics;
public module core::primitives;
public module core::option;
public module core::result;
public module core::panic_unwind;
public module core::slice;
public module core::ptr;
public module core::mem;
public module core::marker;
public module core::clone;
public module core::default;
public module core::cmp;
public module core::convert;
public module core::iter;
public module core::ops;
public module core::hash;
public module core::drop;
public module core::varargs;
Most of core is in the prelude already. Option, Result, Slice, Range, Iterator, the panic helpers, the primitive methods, and the traits the compiler reasons about by identity — Drop, Copy, Send, Sync, Eq, Ord, Clone — all resolve without an import. The rest you import explicitly.
| File | What it holds | Import |
|---|---|---|
option | Option<T> | (prelude) |
result | Result<T, E> | (prelude) |
slice | Slice<T>, the borrowed contiguous view | (prelude) |
ptr | NonNull<T> | import std::core::ptr; |
iter | Iterator and its adapters | (prelude) |
ops | Range, RangeInclusive, Step, and the operator traits | (prelude) |
cmp | Ordering, Eq, Ord, min, max, clamp | (prelude) |
convert | From, Into, TryFrom, TryInto, ConversionError | import std::core::convert; |
hash | Hash, Hasher, DefaultHasher, Fnv128Hasher | import std::core::hash; |
marker | Copy, Send, Sync | (prelude) |
clone | Clone | (prelude) |
default | Default | import std::core::default; |
drop | Drop | (prelude) |
mem | copy, zero, swap, transmute, the alignment helpers | import std::core::mem; |
panic_unwind | catch_unwind, PanicInfo | import std::core::panic_unwind; |
primitives | Methods on i32, char, string, ... | (prelude) |
varargs | VaArgs, the args... bucket | (prelude) |
intrinsics | The compiler-lowered primitives everything else is built on | (prelude) |
Panicking
These four live at the root of core (std::core), which is in the prelude. panic is typed never, so the compiler treats the call as divergent structurally rather than special-casing the symbol name; it forwards to the panic intrinsic.
| Function | Behaviour |
|---|---|
panic | Abort with the message and source location. |
assert | Panic when the condition is false. Always checked. |
unreachable | For a path you believe cannot be taken. |
todo | For a function you have not written yet. |
Pass the compiler-provided FILE and LINE to panic; the helpers do it for you. To catch a panic instead of aborting, see panic_unwind.
function panic(message: string, loc: intrinsics::Location) -> never;
function panic(message: string, loc: intrinsics::Location) -> never;Panic with message, reporting the place loc: intrinsics::location() written at the call, or in a ![caller_location] function its caller's. The helpers below do that automatically.
function assert(condition: boolean, message: string) -> void;
function assert(condition: boolean, message: string) -> void;Panic if condition is false. Always checked.
function unreachable() -> never;
function unreachable() -> never;Mark a code path the programmer believes cannot be reached. If it ever is, the program aborts with a clear message rather than executing wrong code.
function todo() -> never;
function todo() -> never;Stub a function you'll implement later. Reading better than inlining panic("not yet implemented", ...) with the place is its whole job.
public function name() { ... }
public:Visible to any module that imports this one - the default for top-level items and for struct fields, so it is mostly written to be explicit. public: opens a visibility block that groups the members after it.
public module collections::array;In a directory's _module.cryo aggregator, lists a submodule and whether it is public. Importing the directory's namespace sees only the modules declared public here.
namespace std::core::varargs;VaArgs: a safe, typed wrapper over a C-ABI variadic argument list.
module pagestart..endA half-open range - start up to but excluding end - and sugar for Range::new(start, end): an iterator usable in for, and an ordinary value anywhere else. It binds looser than arithmetic, so a..b + 1 is a..(b + 1).
type struct Range<T> {
start: T;
end: T;
}for (mut i: i32 = 0; i < n; i++) { ... }
for (item in iterable) { ... }Two forms. The C-style for (init; condition; step) scopes its loop variable to the body. for (x in expr) iterates anything with next() -> Option<T>: an iterator, a range such as 0..n, an Array or Slice (their iter() is inserted for you), or a fixed-size array.
type trait Iterator {
type Item;
next(mut &this) -> Option<This::Item>;
count(mut &this) -> u64;
fold<Acc>(mut &this, initial: Acc, f: (Acc, This::Item) -> Acc) -> Acc;
for_each(mut &this, f: (This::Item) -> void) -> void;
take(this, n: u64) -> TakeIter<This>;
skip(this, n: u64) -> SkipIter<This>;
map<B>(this, f: (This::Item) -> B) -> MapIter<This, B>;
filter(this, pred: (&This::Item) -> boolean) -> FilterIter<This>;
chain<J>(this, other: J) -> ChainIter<This, J>;
// … 7 more
}type trait Ord : Eq {
compare(&this, other: &This) -> Ordering;
}Types that have a total order: every pair is Less, Equal, or Greater. compare must be consistent with equals: if a.equals(b) then a.compare(b) == Equal.
Ord::compare(&this, other: &This) -> Orderingtype struct Slice<T> {
ptr: T*;
length: u64;
}Slice<T>: a borrowed view into a contiguous sequence.
std::core::slicetype trait Copy {}Types whose values can be duplicated by a bitwise copy. Integers, floats, pointers, and pure-POD structs qualify; anything that owns resources (heap storage, file handles, refcount) does not.
std::core::markertype trait Send {}Types that are safe to transfer between threads. An owning value of a Send type may cross a thread boundary without violating memory safety.
type trait Sync {}Types that are safe to share between threads by shared reference. T: Sync if &T: Send.
type trait Eq {
equals(&this, other: &This) -> boolean;
}Types that support value equality. equals(a, b) must be reflexive, symmetric, and transitive.
type struct NonNull<T> {
ptr: T*;
}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 struct RangeInclusive<T> {
start: T;
end: T;
exhausted: boolean;
}type trait Step : Ord {
successor(&this) -> This;
}type enum Ordering {
Less;
Equal;
Greater;
}function min<T>(a: T, b: T) -> T
where T: Ordfunction max<T>(a: T, b: T) -> T
where T: Ordfunction clamp<T>(value: T, lo: T, hi: T) -> T
where T: OrdConstrain value to [lo, hi]. Panics if lo > hi; the caller gave a nonsense range.
type trait From<T> {
static from(value: T) -> This;
}type trait Into<T> {
into(&this) -> T;
}type trait TryFrom<T> {
static try_from(value: T) -> Result<This, ConversionError>;
}type trait TryInto<T> {
try_into(&this) -> Result<T, ConversionError>;
}type struct ConversionError {
reason: string;
}What went wrong in a TryFrom / TryInto. Carries a short static message describing the failure; richer diagnostics are a caller concern.
type trait Hasher {
write_u8(mut &this, value: u8) -> void;
write_bytes(mut &this, bytes: Slice<u8>) -> void;
write_uint(mut &this, value: u128, byte_count: u32) -> void;
fold<T>(mut &this, value: T) -> void;
finish(&this) -> u64;
}type struct DefaultHasher {
state: u64;
}64-bit FNV-1a. Simple; not DoS-resistant. Fine as a HashMap default when inputs are trusted.
std::core::hashtype struct Fnv128Hasher {
state: u128;
}128-bit FNV-1a. Same construction as DefaultHasher, twice the width.
type trait Default {
static default() -> This;
}Default: a canonical "zero value" for a type.
std::core::defaulttype struct PanicInfo {
msg: string;
file: string;
line: u32;
}The payload a caught panic reports: the panic message and the source location it was raised from. Mirrors the fields the runtime funnel (runtime/panic/unwind) stashes when it raises.
primitive stringNUL-terminated raw string (u8*), matching the C ABI. Length-typed text is Str / String.
type struct VaArgs {
ptr: void*;
}Single-pointer wrapper over the active va_list. ptr is the raw va_list pointer - the args identifier the compiler binds in a variadic body.
function name(count: i32, args...)A variadic parameter: the trailing bucket receives any number of further arguments with C's calling convention, so the function stays ABI-compatible with the printf family. Wrap the bucket in VaArgs to read typed values from it.
function panic(message: string, loc: intrinsics::Location) -> neverPanic with message, reporting the place loc: intrinsics::location() written at the call, or in a ![caller_location] function its caller's. The helpers below do that automatically.
primitive neverThe type of an expression that does not return. Only valid as a return type, written by functions that always panic, trap, or diverge. 0 bytes.
primitive typefunction assert(condition: boolean, message: string) -> voidPanic if condition is false. Always checked.
function unreachable() -> neverMark a code path the programmer believes cannot be reached. If it ever is, the program aborts with a clear message rather than executing wrong code.
std::corefunction todo() -> neverStub a function you'll implement later. Reading better than inlining panic("not yet implemented", ...) with the place is its whole job.
function name<T>(param: Type) -> Ret { ... }Declares a function. Parameters are written name: Type and are never inferred; the return type follows -> and defaults to void. Declaration order does not matter: every signature is collected before any body is checked, so functions may call each other freely and recurse.
function panic(message: string, loc: intrinsics::Location) -> neverPanic with message, reporting the place loc: intrinsics::location() written at the call, or in a ![caller_location] function its caller's. The helpers below do that automatically.
(parameter) message: string(parameter) loc: intrinsics::Locationfunction assert(condition: boolean, message: string) -> voidPanic if condition is false. Always checked.
(parameter) condition: booleanfunction unreachable() -> neverMark a code path the programmer believes cannot be reached. If it ever is, the program aborts with a clear message rather than executing wrong code.
function todo() -> neverStub a function you'll implement later. Reading better than inlining panic("not yet implemented", ...) with the place is its whole job.