0

The following code doesn't compile because assigning a to b returns: expected struct std::string::String, found &std::string::String. I thought &s was &Option and not &Option<&T>. It seems that &s is behaving exactly like s.as_ref(). What's the reason behind this behavior?

fn main() {
    let s = Some("string".to_string());
    if let Some(a) = &s {
        let b: String = a;
        println!("{}", b);
    }
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Danilo Souza Morães
  • 1,481
  • 13
  • 18

0 Answers0