doc.rust-lang.org
Diverging functions Diverging functions never return. They are marked using !, which is an empty type. fn foo() -> ! { panic!("This call never returns."); } As opposed to all the other types, this one cannot be instantiated, because the set...
Rust by Example
Book
2024-01-01
~1 min read
doc.rust-lang.org
Higher Order Functions Rust provides Higher Order Functions (HOF). These are functions that take one or more functions and/or produce a more useful function. HOFs and lazy iterators give Rust its functional flavor. fn is_odd(n: u32) -> bool...
Rust by Example
Book
2024-01-01
~1 min read
doc.rust-lang.org
Searching through iterators Iterator::find is a function which iterates over an iterator and searches for the first value which satisfies some condition. If none of the values satisfy the condition, it returns None. Its signature: pub trait...
Rust by Example
Book
2024-01-01
~1 min read
doc.rust-lang.org
Iterator::any Iterator::any is a function which when passed an iterator, will return true if any element satisfies the predicate. Otherwise false. Its signature: pub trait Iterator { // The type being iterated over. type Item; // `any` take...
Rust by Example
Book
2024-01-01
~1 min read
doc.rust-lang.org
Examples in std This section contains a few examples of using closures from the std library.
Rust by Example
Book
2024-01-01
~1 min read
doc.rust-lang.org
As output parameters Closures as input parameters are possible, so returning closures as output parameters should also be possible. However, anonymous closure types are, by definition, unknown, so we have to use impl Trait to return them. T...
Rust by Example
Book
2024-01-01
~1 min read
doc.rust-lang.org
Input functions Since closures may be used as arguments, you might wonder if the same can be said about functions. And indeed they can! If you declare a function that takes a closure as parameter, then any function that satisfies the trait...
Rust by Example
Book
2024-01-01
~1 min read
doc.rust-lang.org
Type anonymity Closures succinctly capture variables from enclosing scopes. Does this have any consequences? It surely does. Observe how using a closure as a function parameter requires generics, which is necessary because of how they are d...
Rust by Example
Book
2024-01-01
~1 min read
doc.rust-lang.org
As input parameters While Rust chooses how to capture variables on the fly mostly without type annotation, this ambiguity is not allowed when writing functions. When taking a closure as an input parameter, the closure's complete type must b...
Rust by Example
Book
2024-01-01
~2 min read
doc.rust-lang.org
Capturing Closures are inherently flexible and will do what the functionality requires to make the closure work without annotation. This allows capturing to flexibly adapt to the use case, sometimes moving and sometimes borrowing. Closures...
Rust by Example
Book
2024-01-01
~2 min read
doc.rust-lang.org
Closures Closures are functions that can capture the enclosing environment. For example, a closure that captures the x variable: |val| val + x The syntax and capabilities of closures make them very convenient for on the fly usage. Calling a...
Rust by Example
Book
2024-01-01
~1 min read
doc.rust-lang.org
Associated functions & Methods Some functions are connected to a particular type. These come in two forms: associated functions, and methods. Associated functions are functions that are defined on a type generally, while methods are associa...
Rust by Example
Book
2024-01-01
~2 min read
doc.rust-lang.org
Functions Functions are declared using the fn keyword. Its arguments are type annotated, just like variables, and, if the function returns a value, the return type must be specified after an arrow ->. The final expression in the function wi...
Rust by Example
Book
2024-01-01
~1 min read
doc.rust-lang.org
while let Similar to if let, while let can make awkward match sequences more tolerable. Consider the following sequence that increments i: // Make `optional` of type `Option<i32>` let mut optional = Some(0); // Repeatedly try this test. loo...
Rust by Example
Book
2024-01-01
~1 min read
doc.rust-lang.org
let-else 🛈 stable since: rust 1.65 🛈 you can target specific edition by compiling like this rustc --edition=2021 main.rs With let-else, a refutable pattern can match and bind variables in the surrounding scope like a normal let, or else div...
Rust by Example
Book
2024-01-01
~1 min read
doc.rust-lang.org
if let For some use cases, when matching enums, match is awkward. For example: // Make `optional` of type `Option<i32>` let optional = Some(7); match optional { Some(i) => println!("This is a really long string and `{:?}`", i), _ => {}, //...
Rust by Example
Book
2024-01-01
~2 min read
doc.rust-lang.org
Binding Indirectly accessing a variable makes it impossible to branch and use that variable without re-binding. match provides the @ sigil for binding values to names: // A function `age` which returns a `u32`. fn age() -> u32 { 15 } fn mai...
Rust by Example
Book
2024-01-01
~1 min read
doc.rust-lang.org
Guards A match guard can be added to filter the arm. #[allow(dead_code)] enum Temperature { Celsius(i32), Fahrenheit(i32), } fn main() { let temperature = Temperature::Celsius(35); // ^ TODO try different values for `temperature` match temp...
Rust by Example
Book
2024-01-01
~1 min read
doc.rust-lang.org
structs Similarly, a struct can be destructured as shown: fn main() { struct Foo { x: (u32, u32), y: u32, } // Try changing the values in the struct to see what happens let foo = Foo { x: (1, 2), y: 3 }; match foo { Foo { x: (1, b), y } =>...
Rust by Example
Book
2024-01-01
~1 min read
doc.rust-lang.org
pointers/ref For pointers, a distinction needs to be made between destructuring and dereferencing as they are different concepts which are used differently from languages like C/C++. • Dereferencing uses * • Destructuring uses &, ref, and r...
Rust by Example
Book
2024-01-01
~1 min read