ptr
import std::core::ptr; · source
Types
NonNull<T>
type struct NonNull<T> {
ptr: T*;
static new(raw: T*) -> NonNull<T>;
as_ptr(&this) -> T*;
offset(&this, count: i64) -> NonNull<T>;
cast<U>(&this) -> NonNull<U>;
}
A raw pointer that is guaranteed non-null by construction. Check once at the ownership boundary and let everything downstream rely on the invariant. The owning smart pointers — Box, Rc, Arc — build on this and live in alloc.
import std::core::ptr;
mut p: NonNull<i32> = NonNull::<i32>::new(raw); // aborts if `raw` is null
const q: NonNull<i32> = p.offset(3); // 3 elements, not 3 bytes
cast<U> cannot check that the alignment requirements line up; that is on you.
Properties
| Property | Type |
|---|---|
ptr | T* |
Constructors
static new(raw: T*) -> NonNull<T>;
static new(raw: 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_ptr(&this) -> T*;
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.
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> {
ptr: T*;
}type parameter TNonNull.ptr: T*type parameter Tstatic 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 NonNull::new(raw: 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.
(parameter) raw: T*type struct NonNull<T> {
ptr: T*;
}NonNull::as_ptr(&this) -> 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.
NonNull::offset(&this, count: i64) -> NonNull<T>Offset by count elements (not bytes). Negative values walk backwards.
(parameter) count: i64NonNull::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.
type parameter Utype parameter Uimport 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).
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.
mut p: NonNull<i32>static NonNull::new(raw: 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::ptrconst 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 q: NonNull<i32>mut p: NonNull<i32>NonNull::offset(&this, count: i64) -> NonNull<T>Offset by count elements (not bytes). Negative values walk backwards.
type struct Box<T, A = GlobalAlloc> where A: Allocator {
ptr: T*;
alloc: A;
}Box<T, A>: single-owner heap allocation.
std::alloc::boxtype struct Rc<T, A = GlobalAlloc> where A: Allocator {
inner: RcInner<T, A>*;
}Rc<T, A>: single-threaded reference counting, with weak references.
std::alloc::rctype struct Arc<T, A = GlobalAlloc> where A: Allocator {
inner: ArcInner<T, A>*;
}Arc<T, A>: atomic reference counting, with weak references.
std::alloc::arcNonNull::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.
NonNull.ptr: T*NonNull::as_ptr(&this) -> T*