Rust Search

Find the best content on Rust, curated by the community; a search engine for Rustaceans.
doc.rust-lang.org
Alternate/custom key types Any type that implements the Eq and Hash traits can be a key in HashMap. This includes: • bool (though not very useful since there are only two possible keys) • int, uint, and all variations thereof • String and &...
Rust by Example Book 2024-01-01 ~1 min read
doc.rust-lang.org
HashMap Where vectors store values by an integer index, HashMaps store values by key. HashMap keys can be booleans, integers, strings, or any other type that implements the Eq and Hash traits. More on this in the next section. Like vectors,...
Rust by Example Book 2024-01-01 ~1 min read
doc.rust-lang.org
panic! The panic! macro can be used to generate a panic and start unwinding its stack. While unwinding, the runtime will take care of freeing all the resources owned by the thread by calling the destructor of all its objects. Since we are d...
Rust by Example Book 2024-01-01 ~1 min read
doc.rust-lang.org
?
? Chaining results using match can get pretty untidy; luckily, the ? operator can be used to make things pretty again. ? is used at the end of an expression returning a Result, and is equivalent to a match expression, where the Err(err) bra...
Rust by Example Book 2024-01-01 ~1 min read
doc.rust-lang.org
Result We've seen that the Option enum can be used as a return value from functions that may fail, where None can be returned to indicate failure. However, sometimes it is important to express why an operation failed. To do this we have the...
Rust by Example Book 2024-01-01 ~1 min read
doc.rust-lang.org
Option Sometimes it's desirable to catch the failure of some parts of a program instead of calling panic!; this can be accomplished using the Option enum. The Option<T> enum has two variants: • None, to indicate failure or lack of value, an...
Rust by Example Book 2024-01-01 ~1 min read
doc.rust-lang.org
Strings The two most used string types in Rust are String and &str. A String is stored as a vector of bytes (Vec<u8>), but guaranteed to always be a valid UTF-8 sequence. String is heap allocated, growable and not null terminated. &str is a...
Rust by Example Book 2024-01-01 ~4 min read
doc.rust-lang.org
Vectors Vectors are re-sizable arrays. Like slices, their size is not known at compile time, but they can grow or shrink at any time. A vector is represented using 3 parameters: • pointer to the data • length • capacity The capacity indicat...
Rust by Example Book 2024-01-01 ~1 min read
doc.rust-lang.org
Box, stack and heap All values in Rust are stack allocated by default. Values can be boxed (allocated on the heap) by creating a Box<T>. A box is a smart pointer to a heap allocated value of type T. When a box goes out of scope, its destruc...
Rust by Example Book 2024-01-01 ~1 min read
doc.rust-lang.org
Std library types The std library provides many custom types which expands drastically on the primitives. Some of these include: • growable Strings like: "hello world" • growable vectors: [1, 2, 3] • optional types: Option<i32> • error hand...
Rust by Example Book 2024-01-01 ~1 min read
doc.rust-lang.org
Iterating over Results An Iter::map operation might fail, for example: fn main() { let strings = vec!["tofu", "93", "18"]; let numbers: Vec<_> = strings .into_iter() .map(|s| s.parse::<i32>()) .collect(); println!("Results: {:?}", numbers);...
Rust by Example Book 2024-01-01 ~1 min read
doc.rust-lang.org
Wrapping errors An alternative to boxing errors is to wrap them in your own error type. use std::error; use std::error::Error; use std::num::ParseIntError; use std::fmt; type Result<T> = std::result::Result<T, DoubleError>; #[derive(Debug)]...
Rust by Example Book 2024-01-01 ~1 min read
doc.rust-lang.org
Other uses of ? Notice in the previous example that our immediate reaction to calling parse is to map the error from a library error into a boxed error: .and_then(|s| s.parse::<i32>()) .map_err(|e| e.into()) Since this is a simple and commo...
Rust by Example Book 2024-01-01 ~1 min read
doc.rust-lang.org
Boxing errors A way to write simple code while preserving the original errors is to Box them. The drawback is that the underlying error type is only known at runtime and not statically determined. The stdlib helps in boxing our errors by ha...
Rust by Example Book 2024-01-01 ~1 min read
doc.rust-lang.org
Defining an error type Sometimes it simplifies the code to mask all of the different errors with a single type of error. We'll show this with a custom error. Rust allows us to define our own error types. In general, a "good" error type: • R...
Rust by Example Book 2024-01-01 ~1 min read
doc.rust-lang.org
Pulling Results out of Options The most basic way of handling mixed error types is to just embed them in each other. use std::num::ParseIntError; fn double_first(vec: Vec<&str>) -> Option<Result<i32, ParseIntError>> { vec.first().map(|first...
Rust by Example Book 2024-01-01 ~1 min read
doc.rust-lang.org
Multiple error types The previous examples have always been very convenient; Results interact with other Results and Options interact with other Options. Sometimes an Option needs to interact with a Result, or a Result<T, Error1> needs to i...
Rust by Example Book 2024-01-01 ~1 min read
doc.rust-lang.org
Introducing ? Sometimes we just want the simplicity of unwrap without the possibility of a panic. Until now, unwrap has forced us to nest deeper and deeper when what we really wanted was to get the variable out. This is exactly the purpose...
Rust by Example Book 2024-01-01 ~1 min read
doc.rust-lang.org
Early returns In the previous example, we explicitly handled the errors using combinators. Another way to deal with this case analysis is to use a combination of match statements and early returns. That is, we can simply stop executing the...
Rust by Example Book 2024-01-01 ~1 min read
doc.rust-lang.org
aliases for Result How about when we want to reuse a specific Result type many times? Recall that Rust allows us to create aliases. Conveniently, we can define one for the specific Result in question. At a module level, creating aliases can...
Rust by Example Book 2024-01-01 ~1 min read
"Because it is designed not to own. If you need an owning pointer, use Box. This is like asking "why there is no chocolate mousse in this burger?". Ch..."

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.