0
0
Rustprogramming~20 mins

Destructuring patterns in Rust - Mini Project: Build & Apply

Choose your learning style9 modes available
Destructuring Patterns in Rust
📖 Scenario: You are working on a simple Rust program that manages information about a person. You want to extract parts of the data easily using destructuring patterns.
🎯 Goal: Learn how to use destructuring patterns in Rust to extract values from tuples and structs.
📋 What You'll Learn
Create a tuple with exact values
Create a struct with exact fields and values
Use destructuring patterns to extract values from the tuple and struct
Print the extracted values exactly as instructed
💡 Why This Matters
🌍 Real World
Destructuring patterns help you easily get parts of complex data like user info, coordinates, or settings in real Rust programs.
💼 Career
Many Rust jobs require working with data structures and extracting values cleanly. Destructuring is a common and useful skill.
Progress0 / 4 steps
1
Create a tuple with person data
Create a tuple called person with these exact values: "Alice", 30, and 5.5.
Rust
Hint

Use parentheses to create a tuple: ("Alice", 30, 5.5)

2
Create a struct for person info
Define a struct called Person with fields name (string slice), age (u8), and height (f32). Then create a variable p of type Person with name as "Bob", age as 25, and height as 6.0.
Rust
Hint

Use struct Person { name: &str, age: u8, height: f32 } and then create p with the given values.

3
Destructure the tuple and struct
Use destructuring patterns to extract the values from person into variables name, age, and height. Also destructure p into variables p_name, p_age, and p_height.
Rust
Hint

Use let (name, age, height) = person; for the tuple and let Person { name: p_name, age: p_age, height: p_height } = p; for the struct.

4
Print the extracted values
Print the extracted variables exactly as follows, each on its own line:
name
age
height
p_name
p_age
p_height
Use println! macro for printing.
Rust
Hint

Use println!("{}", variable); for each variable on its own line.