Skip to content

Commit

Permalink
Match and Error Example Added
Browse files Browse the repository at this point in the history
  • Loading branch information
ramagururadhakrishnan authored May 8, 2024
1 parent 3d10872 commit 7af77a5
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Assets/Lectures/RL5.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,41 @@ fn divide(x: f64, y: f64) -> Result<f64, &'static str> {
}
```

### Match and Error
```
use std::io;
fn main() {
println!("Select an option:");
println!("1. Option 1");
println!("2. Option 2");
println!("3. Option 3");
let mut choice = String::new();
io::stdin()
.read_line(&mut choice)
.expect("Failed to read line");
// Parse the input to an integer
let choice: u32 = match choice.trim().parse() {
Ok(num) => num,
Err(_) => {
println!("Invalid input. Please enter a number.");
return;
}
};
// Match the choice and print corresponding messages
match choice {
1 => println!("You chose Option 1"),
2 => println!("You chose Option 2"),
3 => println!("You chose Option 3"),
_ => println!("Invalid choice"),
}
}
```

## Practice
You are tasked with implementing a simple inventory management system for a store. Define a structure named Item to represent an item in the inventory. Each item should have the following attributes:

Expand Down

0 comments on commit 7af77a5

Please sign in to comment.