[Rust] ref keyword for borrow value

https://doc.rust-lang.org/std/keyword.ref.html

struct Point {
    x: i32,
    y: i32,
}

fn main() {
    let y: Option<Point> = Some(Point { x: 100, y: 200 });

    match y {
        Some(ref p) => println!("Co-ordinates are {},{} ", p.x, p.y), // have to use ref keyword to borrow the value
        _ => panic!("no match!"),
    }
    y; // after using ref, you can borrow the value y.
}

 

posted @ 2024-03-04 21:57  Zhentiw  阅读(4)  评论(0编辑  收藏  举报