Avatar ·

How to get a reference to a value inside Option<T> in Rust without moving?

Hello colleagues! I'm new to Rust and encountered a problem that seems very strange to me. I have a struct that contains a field of type `Option`. I want to write a method that returns a reference to the string inside this Option if it exists, or something like `None` if it doesn't. Here's my code: ```rust struct Person { name: Option, } impl Person { fn get_name(&self) -> &str { // How do I get a reference to the String inside Option here? // self.name.unwrap() // Error: cannot move out of borrowed context // &self.name.unwrap() // Also doesn't work } } ``` When I try to use `unwrap()`, the compiler says I'm trying to move the value out of `self.name`, but `self` is only borrowed by reference.