rustlings

solving rustlings ft. dracuxan
git clone [email protected]:dracuxan/rustlings.git
Log | Files | Refs

options3.rs (694B)


      1 #[derive(Debug)]
      2 struct Point {
      3     x: i32,
      4     y: i32,
      5 }
      6 
      7 fn main() {
      8     let optional_point = Some(Point { x: 100, y: 200 });
      9 
     10     // Solution 1: Matching over the `Option` (not `&Option`) but without moving
     11     // out of the `Some` variant.
     12     match optional_point {
     13         Some(ref p) => println!("Coordinates are {},{}", p.x, p.y),
     14         //   ^^^ added
     15         _ => panic!("No match!"),
     16     }
     17 
     18     // Solution 2: Matching over a reference (`&Option`) by added `&` before
     19     // `optional_point`.
     20     match &optional_point {
     21         //^ added
     22         Some(p) => println!("Coordinates are {},{}", p.x, p.y),
     23         _ => panic!("No match!"),
     24     }
     25 
     26     println!("{optional_point:?}");
     27 }