What reference does
reference is a primitive type exposed by the std module of the Rust standard library. It is part of the public, stable API and you can rely on it from any edition of Rust.
reference is a built-in primitive type. The language and the compiler understand it directly — there is no `impl` block in std declaring its layout. Inherent methods on the type are listed in its documentation page. The full canonical path is std::reference; bring it into scope with use std::reference; or refer to it by its full path.
When to use it
Use reference when its representation matches your data: width, signedness, or platform-specific layout. Reach for the inherent methods on the type for arithmetic and conversion helpers.
Annotated examples
Working with reference literals and arithmetic
fn demo() {
// Primitive types are built into the language. They have inherent
// methods (`u32::checked_add`, `f64::sqrt`, `bool::then_some`, etc.)
// — these are the easiest place to start when you need behaviour
// beyond plain operators.
let value: reference = Default::default();
println!("{:?}", value);
}
Primitive types in Rust are not magic — they have plain inherent-method APIs you can browse like any other type, and they implement standard traits like Default, Clone, Debug, and PartialEq.
For deeper background, see the canonical Rust patterns reference for the broader context behind this section.
Converting between reference and other types
fn demo() {
// Use From / Into for infallible conversions and TryFrom / TryInto
// for fallible ones (narrowing, e.g. u64 -> u32).
let value: reference = Default::default();
println!("{:?}", value);
}
Numeric conversions in Rust are explicit by design — every cast, narrowing, or sign change must be written out. The From / TryFrom split tells you whether a conversion can fail at runtime.
Common pitfalls
Plain `+`, `-`, `*` panic in debug builds and wrap in release builds on overflow. For predictable behaviour, reach for `checked_*`, `wrapping_*`, `saturating_*`, or `overflowing_*` arithmetic methods explicitly.
For deeper background, see an in-depth Rust idioms cheat sheet for the broader context behind this section.
Performance & threading notes
Stored on the stack in registers when possible. Arithmetic compiles to single CPU instructions on every modern target. SIMD types in `std::simd` give explicit access to vector instructions when you need higher throughput.