Destructuring
In this page:
let (a, b) = tuple;
let Name { field1, field2 } = value;
let [first, .., last] = array;
Destructuring a Tuple
A let statement can destructure a tuple directly into multiple named variables in one line, instead of accessing each element with .0, .1, and so on.
Example: Destructuring a Tuple
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `point`, set to `(3, 7)`
let point = (3, 7);
// Destructure `point` into `x, y`
let (x, y) = point;
// Print "x={}, y={}" to the console, substituting in `x, y` (with a trailing newline)
println!("x={}, y={}", x, y);
}
Login to try C/C++/Java/PHP code in the editor
Destructuring a Struct
Struct fields can be destructured into local variables of the same names directly in a let statement, using the struct's field names as the pattern.
Example: Destructuring a Struct
// Define a struct `Point` to group related fields together
struct Point {
x: i32,
y: i32,
}
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `p`, set to `Point { x: 5, y: 9 }`
let p = Point { x: 5, y: 9 };
let Point { x, y } = p;
// Print "x={}, y={}" to the console, substituting in `x, y` (with a trailing newline)
println!("x={}, y={}", x, y);
}
Login to try C/C++/Java/PHP code in the editor
Ignoring Fields with ..
When you only need a few fields from a larger struct, .. in the pattern tells Rust to ignore all remaining fields without naming them individually.
Example: Ignoring Fields with ..
// Define a struct `Config` to group related fields together
struct Config {
width: u32,
height: u32,
fullscreen: bool,
}
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `cfg`, set to `Config { width: 1920, height: 1080, fullscreen: true }`
let cfg = Config { width: 1920, height: 1080, fullscreen: true };
let Config { width, .. } = cfg;
// Print "Width: {}" to the console, substituting in `width` (with a trailing newline)
println!("Width: {}", width);
}
Login to try C/C++/Java/PHP code in the editor
Discarding Values with _
The underscore pattern _ matches any value while discarding it, useful in destructuring or function parameters when a value must be present but is not actually needed.
Example: Discarding Values with _
// Entry point: execution of the program starts here
fn main() {
// Destructure `(1, 2, 3)` into `first, _, third`
let (first, _, third) = (1, 2, 3);
// Print "first={}, third={}" to the console, substituting in `first, third` (with a trailing newline)
println!("first={}, third={}", first, third);
}
Login to try C/C++/Java/PHP code in the editor
- Trying to destructure only some fields of a struct without using
..to explicitly ignore the rest, which is a compile error. - Forgetting the
_pattern can be used to intentionally discard a value you don't need during destructuring. - Assuming destructuring a reference automatically dereferences everything -- sometimes an explicit
&pattern or.clone()is needed.
- Destructuring extracts multiple named values from a tuple, struct, or enum in a single
letormatch. - The
..pattern can be used to ignore remaining fields you don't care about. - The
_pattern matches and discards any single value without binding it to a name. - Destructuring works in
letstatements, function parameters, and match arms alike.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: