Rust Search

Find the best content on Rust, curated by the community; a search engine for Rustaceans.
doc.rust-lang.org
Mutability Mutable data can be mutably borrowed using &mut T. This is called a mutable reference and gives read/write access to the borrower. In contrast, &T borrows the data via an immutable reference, and the borrower can read the data bu...
Rust by Example Book 2024-01-01 ~1 min read
doc.rust-lang.org
Borrowing Most of the time, we'd like to access data without taking ownership over it. To accomplish this, Rust uses a borrowing mechanism. Instead of passing objects by value (T), objects can be passed by reference (&T). The compiler stati...
Rust by Example Book 2024-01-01 ~1 min read
doc.rust-lang.org
Partial moves Within the destructuring of a single variable, both by-move and by-reference pattern bindings can be used at the same time. Doing this will result in a partial move of the variable, which means that parts of the variable will...
Rust by Example Book 2024-01-01 ~1 min read
doc.rust-lang.org
Mutability Mutability of data can be changed when ownership is transferred. fn main() { let immutable_box = Box::new(5u32); println!("immutable_box contains {}", immutable_box); // Mutability error //*immutable_box = 4; // *Move* the box, c...
Rust by Example Book 2024-01-01 ~1 min read
doc.rust-lang.org
Ownership and moves Because variables are in charge of freeing their own resources, resources can only have one owner. This prevents resources from being freed more than once. Note that not all variables own resources (e.g. references). Whe...
Rust by Example Book 2024-01-01 ~1 min read
doc.rust-lang.org
RAII Variables in Rust do more than just hold data in the stack: they also own resources, e.g. Box<T> owns memory in the heap. Rust enforces RAII (Resource Acquisition Is Initialization), so whenever an object goes out of scope, its destruc...
Rust by Example Book 2024-01-01 ~1 min read
doc.rust-lang.org
Scoping rules Scopes play an important part in ownership, borrowing, and lifetimes. That is, they indicate to the compiler when borrows are valid, when resources can be freed, and when variables are created or destroyed.
Rust by Example Book 2024-01-01 ~1 min read
doc.rust-lang.org
Testcase: unit clarification A useful method of unit conversions can be examined by implementing Add with a phantom type parameter. The Add trait is examined below: // This construction would impose: `Self + RHS = Output` // where RHS defau...
Rust by Example Book 2024-01-01 ~1 min read
doc.rust-lang.org
Phantom type parameters A phantom type parameter is one that doesn't show up at runtime, but is checked statically (and only) at compile time. Data types can use extra generic type parameters to act as markers or to perform type checking at...
Rust by Example Book 2024-01-01 ~1 min read
doc.rust-lang.org
Associated types The use of "Associated types" improves the overall readability of code by moving inner types locally into a trait as output types. Syntax for the trait definition is as follows: // `A` and `B` are defined in the trait via t...
Rust by Example Book 2024-01-01 ~1 min read
doc.rust-lang.org
The Problem A trait that is generic over its container type has type specification requirements - users of the trait must specify all of its generic types. In the example below, the Contains trait allows the use of the generic types A and B...
Rust by Example Book 2024-01-01 ~1 min read
doc.rust-lang.org
Associated items "Associated Items" refers to a set of rules pertaining to items of various types. It is an extension to trait generics, and allows traits to internally define new items. One such item is called an associated type, providing...
Rust by Example Book 2024-01-01 ~1 min read
doc.rust-lang.org
New Type Idiom The newtype idiom gives compile time guarantees that the right type of value is supplied to a program. For example, a function that measures distance in miles, must be given a value of type Miles. struct Miles(f64); struct Ki...
Rust by Example Book 2024-01-01 ~1 min read
doc.rust-lang.org
Where clauses A bound can also be expressed using a where clause immediately before the opening {, rather than at the type's first mention. Additionally, where clauses can apply bounds to arbitrary types, rather than just to type parameters...
Rust by Example Book 2024-01-01 ~1 min read
doc.rust-lang.org
Multiple bounds Multiple bounds for a single type can be applied with a +. Like normal, different types are separated with ,. use std::fmt::{Debug, Display}; fn compare_prints<T: Debug + Display>(t: &T) { println!("Debug: `{:?}`", t); print...
Rust by Example Book 2024-01-01 ~1 min read
doc.rust-lang.org
Testcase: empty bounds A consequence of how bounds work is that even if a trait doesn't include any functionality, you can still use it as a bound. Eq and Copy are examples of such traits from the std library. struct Cardinal; struct BlueJa...
Rust by Example Book 2024-01-01 ~1 min read
doc.rust-lang.org
Bounds When working with generics, the type parameters often must use traits as bounds to stipulate what functionality a type implements. For example, the following example uses the trait Display to print and so it requires T to be bound by...
Rust by Example Book 2024-01-01 ~1 min read
doc.rust-lang.org
Traits Of course traits can also be generic. Here we define one which reimplements the Drop trait as a generic method to drop itself and an input. // Non-copyable types. struct Empty; struct Null; // A trait generic over `T`. trait DoubleDr...
Rust by Example Book 2024-01-01 ~1 min read
doc.rust-lang.org
Implementation Similar to functions, implementations require care to remain generic. struct S; // Concrete type `S` struct GenericVal<T>(T); // Generic type `GenericVal` // impl of GenericVal where we explicitly specify type parameters: imp...
Rust by Example Book 2024-01-01 ~1 min read
doc.rust-lang.org
Functions The same set of rules can be applied to functions: a type T becomes generic when preceded by <T>. Using generic functions sometimes requires explicitly specifying type parameters. This may be the case if the function is called whe...
Rust by Example Book 2024-01-01 ~1 min read
"* [`impl Not for !`](https://github.com/rust-lang/rust/pull/91122) (did you guess that "not never" is still "never"?)"

Search tips

Type anything to search across articles, videos (including conference talks), podcasts, research, crates, and Rust API documentation. These operators give you finer control — click an example to try it.

Find pages containing all your words. Pages where the words appear together rank higher.
Quote part of your query to keep those words together as an exact phrase within a larger search.
Wrap the whole query in quotes for a verbatim search that matches text exactly, punctuation and all — perfect for Rust syntax. Needs at least 3 characters.
Put ! first to open the top result immediately. Quote the whole query for a literal leading bang, as in '!Send'. Bangs elsewhere, such as println!, stay literal. Browser and launcher shortcuts can invoke this directly. See search integrations.
Limit results to a single site. Works on its own () too. One site: per search.

Use the tabs and filters above the results to narrow by content type, publication year, and sort order.