Rust Search

Find the best content on Rust, curated by the community; a search engine for Rustaceans.
Syntax In following subsections, we will show how to define macros in Rust. There are three basic ideas: • Patterns and Designators • Overloading • Repetition
doc.rust-lang.org Rust by Example Book 2024-01-01 ~1 min read
Designators The arguments of a macro are prefixed by a dollar sign $ and type annotated with a designator: macro_rules! create_function { // This macro takes an argument of designator `ident` and // creates a function named `$func_name`. //...
doc.rust-lang.org Rust by Example Book 2024-01-01 ~1 min read
Overload Macros can be overloaded to accept different combinations of arguments. In that regard, macro_rules! can work similarly to a match block: // `test!` will compare `$left` and `$right` // in different ways depending on how you invoke...
doc.rust-lang.org Rust by Example Book 2024-01-01 ~1 min read
Repeat Macros can use + in the argument list to indicate that an argument may repeat at least once, or *, to indicate that the argument may repeat zero or more times. In the following example, surrounding the matcher with $(...),+ will matc...
doc.rust-lang.org Rust by Example Book 2024-01-01 ~1 min read
DRY (Don't Repeat Yourself) Macros allow writing DRY code by factoring out the common parts of functions and/or test suites. Here is an example that implements and tests the +=, *= and -= operators on Vec<T>: use std::ops::{Add, Mul, Sub};...
doc.rust-lang.org Rust by Example Book 2024-01-01 ~1 min read
Domain Specific Languages (DSLs) A DSL is a mini "language" embedded in a Rust macro. It is completely valid Rust because the macro system expands into normal Rust constructs, but it looks like a small language. This allows you to define co...
doc.rust-lang.org Rust by Example Book 2024-01-01 ~1 min read
Variadic Interfaces A variadic interface takes an arbitrary number of arguments. For example, println! can take an arbitrary number of arguments, as determined by the format string. We can extend our calculate! macro from the previous secti...
doc.rust-lang.org Rust by Example Book 2024-01-01 ~1 min read
Error handling Error handling is the process of handling the possibility of failure. For example, failing to read a file and then continuing to use that bad input would clearly be problematic. Noticing and explicitly managing those errors s...
doc.rust-lang.org Rust by Example Book 2024-01-01 ~1 min read
panic The simplest error handling mechanism we will see is panic. It prints an error message, starts unwinding the stack, and usually exits the program. Here, we explicitly call panic on our error condition: fn drink(beverage: &str) { // Yo...
doc.rust-lang.org Rust by Example Book 2024-01-01 ~1 min read
abort and unwind The previous section illustrates the error handling mechanism panic. Different code paths can be conditionally compiled based on the panic setting. The current values available are unwind and abort. Building on the prior le...
doc.rust-lang.org Rust by Example Book 2024-01-01 ~1 min read
Option & unwrap In the last example, we showed that we can induce program failure at will. We told our program to panic if we drink a sugary lemonade. But what if we expect some drink but don't receive one? That case would be just as bad, s...
doc.rust-lang.org Rust by Example Book 2024-01-01 ~1 min read
Unpacking options with ? You can unpack Options by using match statements, but it's often easier to use the ? operator. If x is an Option, then evaluating x? will return the underlying value if x is Some, otherwise it will terminate whateve...
doc.rust-lang.org Rust by Example Book 2024-01-01 ~1 min read
Combinators: map match is a valid method for handling Options. However, you may eventually find heavy usage tedious, especially with operations only valid with an input. In these cases, combinators can be used to manage control flow in a mo...
doc.rust-lang.org Rust by Example Book 2024-01-01 ~1 min read
Combinators: and_then map() was described as a chainable way to simplify match statements. However, using map() on a function that returns an Option<T> results in the nested Option<Option<T>>. Chaining multiple calls together can then becom...
doc.rust-lang.org Rust by Example Book 2024-01-01 ~1 min read
Unpacking options and defaults There is more than one way to unpack an Option and fall back on a default if it is None. To choose the one that meets our needs, we need to consider the following: • do we need eager or lazy evaluation? • do w...
doc.rust-lang.org Rust by Example Book 2024-01-01 ~2 min read
Result Result is a richer version of the Option type that describes possible error instead of possible absence. That is, Result<T, E> could have one of two outcomes: • Ok(T): An element T was found • Err(E): An error was found with element...
doc.rust-lang.org Rust by Example Book 2024-01-01 ~1 min read
map for Result Panicking in the previous example's multiply does not make for robust code. Generally, we want to return the error to the caller so it can decide what is the right way to respond to errors. We first need to know what kind of...
doc.rust-lang.org Rust by Example Book 2024-01-01 ~1 min read
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...
doc.rust-lang.org Rust by Example Book 2024-01-01 ~1 min read
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...
doc.rust-lang.org Rust by Example Book 2024-01-01 ~1 min read
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...
doc.rust-lang.org Rust by Example Book 2024-01-01 ~1 min read
"Relying on the programmer to always read, comprehend, and remember the documentation – and then do everything right, every time – is how we get bu..."

Search tips

Type anything to search across articles, videos (including conference talks), podcasts, and research. 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.
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.