pointer
import std::core::pointer; · source
Types
NonNull<T>
type struct NonNull<T> {
private ptr: mut T*;
}
A raw, writable pointer that is guaranteed non-null by construction. Check once at the ownership boundary and let everything downstream rely on the invariant. A NonNull does not own its pointee and releases nothing; the owning smart pointers — Box, Rc, Arc — live in alloc.
import std::core::pointer::{ NonNull };
mut values: [i32] = [1, 2, 3, 4, 5];
const raw: mut i32* = &values[0];
const p: NonNull<i32> = NonNull::<i32>::new(raw); // aborts if `raw` is null
const q: NonNull<i32> = p.offset(3); // 3 elements, not 3 bytes
const back: mut i32* = q.as_pointer();
cast<U> cannot check that the alignment requirements line up; that is on you.
Constructors
static new(raw: mut T*) -> NonNull<T>;
static new(raw: mut T*) -> NonNull<T>;Construct from a raw pointer, aborting if it's null. Check once at the ownership boundary so downstream code can rely on the invariant.
Methods
as_pointer(&this) -> mut T*;
as_pointer(&this) -> mut T*;The pointer, never null.
offset(&this, count: i64) -> NonNull<T>;
offset(&this, count: i64) -> NonNull<T>;Offset by count elements (not bytes). Negative values walk backwards.
cast<U>(&this) -> NonNull<U>;
cast<U>(&this) -> NonNull<U>;Reinterpret this pointer as pointing to a different type. The new type must share alignment requirements with T; the compiler cannot check this.
Intrinsics
intrinsic function read<T>(source: T*) -> T;
intrinsic function read<T>(source: T*) -> T;Takes the value source points to out of its storage, by its bits. The storage still holds those bits and must not release them again: the program writes a new value over it (write, or a store through a raw pointer), frees it without releasing it, or lets it fall past a collection's length. A store through a reference that source was taken from behind releases nothing afterwards. This is the take that *source in a by-value position is refused as (E0453).
intrinsic function write<T>(target: mut T*, value: T) -> void;
intrinsic function write<T>(target: mut T*, value: T) -> void;Stores value into the storage target points to, releasing nothing the storage held: what was there was taken out with read, or was never written. A live value overwritten this way is never released.
intrinsic function drop_in_place<T>(p: T*) -> void;
intrinsic function drop_in_place<T>(p: T*) -> void;Releases the value p points to in place, the way a value leaving scope is released: its type's Drop implementation and then its fields' glue, the glue alone for a type with no implementation, nothing for a Copy value. T takes no bound, so generic code can release a value of any type through it, where calling drop on an unbounded T names no method. The memory itself is not freed, and the value must not be used or released again.
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 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 NonNull<T> {}A raw, writable pointer to a T that is never null: the check is made once, by new, so code holding a NonNull need not repeat it. It does not own the pointee and releases nothing.
type parameter Tprivate type struct Name { ... }
private:Confined to the module that declares it - for items and members alike. A private field or function reached from another module is E0353, and a private type named there is E0503. private: opens a block of such members.
NonNull.ptr: mut T*mut name: Type = value;A mutable binding, reassignable after declaration - mutability is opt-in, const is the default. At module level it declares mutable global state. Before & it marks an exclusive reference or receiver instead.
type parameter Timport 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 NonNull<T> {}A raw, writable pointer to a T that is never null: the check is made once, by new, so code holding a NonNull need not repeat it. It does not own the pointee and releases nothing.
mut values: [i32]const NAME: Type = value;At module level, a true compile-time constant, conventionally SCREAMING_SNAKE_CASE. (mut at module level declares mutable global state instead.)
const raw: mut i32*mut values: [i32]const p: NonNull<i32>static NonNull::new(raw: mut T*) -> NonNull<T>Construct from a raw pointer, aborting if it's null. Check once at the ownership boundary so downstream code can rely on the invariant.
std::core::pointerconst raw: mut i32*const q: NonNull<i32>const p: NonNull<i32>NonNull::offset(&this, count: i64) -> NonNull<T>Offset by count elements (not bytes). Negative values walk backwards.
const back: mut i32*const q: NonNull<i32>type struct Box<T, A = GlobalAllocator> where A: Allocator {}The sole owner of one heap-allocated T, together with the allocator A it came from. Dropping the box drops the value and returns its storage to that allocator. *b reads the value and, on a mut box, writes it.
type struct Rc<T, A = GlobalAllocator> where A: Allocator {}A shared owning handle to one heap-allocated T, for one thread. Cloning an Rc adds a holder rather than copying the value; the value is dropped when the last Rc is dropped, and its block freed through the allocator A once no Weak remains either.
type struct Arc<T, A = GlobalAllocator> where A: Allocator {}A shared, thread-safe owning handle to one heap-allocated T. Cloning an Arc adds a holder rather than copying the value; the value is dropped, and its block freed through the allocator A, when the last Arc is dropped (and the block once no Weak remains).
NonNull::cast<U>(&this) -> NonNull<U>Reinterpret this pointer as pointing to a different type. The new type must share alignment requirements with T; the compiler cannot check this.
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.
(parameter) raw: mut T*&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
(parameter) count: i64intrinsic function name(param: Type) -> Ret;Declares a function the compiler lowers directly to LLVM IR rather than calling. Intrinsics are the contract between the standard library and the compiler (malloc, memcpy, printf, ...); user code does not normally declare its own.
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.
intrinsic function read<T>(source: T*) -> T(parameter) source: T*intrinsic function write<T>(target: mut T*, value: T) -> voidintrinsic function write<T>(target: mut T*, value: T) -> void(parameter) target: mut T*(parameter) value: Tintrinsic function read<T>(source: T*) -> Tintrinsic function drop_in_place<T>(p: T*) -> void(parameter) p: T*const p: NonNull<i32>type trait Drop {
drop(mut &this) -> void;
}A type that releases something when a value of it ends: memory, a file handle, a lock. The compiler calls drop once when an owned value goes out of scope; release a value early with mem::drop(value).
type 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::marker