34 lines
1.1 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// options3.rs
//
// Execute `rustlings hint options3` or use the `hint` watch subcommand for a
// hint.
struct Point {
x: i32,
y: i32,
}
fn main() {
let y: Option<Point> = Some(Point { x: 100, y: 200 });
// match y {
// Some(p) => println!("Co-ordinates are {},{} ", p.x, p.y),
// _ => panic!("no match!"),
// }
// y; // Fix without deleting this line.
// By default, match statements consume all they can, which can sometimes be a problem, when you dont really need the value to be moved and owned: you just need to borrow it.
// The ref keyword can be used to take references to the values in the pattern. This is useful when you want to match on a value, but dont want to take ownership of it.
// The ref keyword can be used in the pattern to take references to the values in the pattern. This is useful when you want to match on a value, but dont want to take ownership of it.
match y {
Some(ref p) => println!("Co-ordinates are {},{} ", p.x, p.y),
_ => panic!("no match!"),
}
y; // Fix without deleting this line.
}