0

Suppose I have the following code:

fn print_type_of<T>(msg: &str, _: &T) {
    println!("type of {}: {}", msg, std::any::type_name::<T>())
}

fn main() {
    let x: &Option<i32> = &Some(32);
    match x {
        Some(y) => print_type_of("y", &y),
        None => println!("None"),
    };
}

link to playground

Could you explain why the program prints: type of y: &i32? Why does pattern matching against &Option<_> give a reference to the wrapped value rather than the value itself?

trent
  • 25,033
  • 7
  • 51
  • 90

0 Answers0