Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
631 views
in Technique[技术] by (71.8m points)

rust - How to get an Option's value or set it if it's empty?

I want to get the name if it's not empty or set a new value. How can I do that?

#[derive(Debug)]
struct App {
    name: Option<String>,
    age: i32,
}

impl App {
    fn get_name<'a>(&'a mut self) -> &'a Option<String> {
        match self.name {
            Some(_) => &self.name,
            None => {
                self.name = Some(String::from("234"));
                &self.name
            }
        }
    }
}

fn main() {
    let mut app = App {
        name: None,
        age: 10,
    };

    println!("{:?} and name is {}", &app, &app.get_name().unwrap())
}

The error I'm getting is:

error[E0507]: cannot move out of borrowed content
  --> src/main.rs:25:44
   |
25 |     println!("{:?} and name is {}", &app, &app.get_name().unwrap())
   |                                            ^^^^^^^^^^^^^^ cannot move out of borrowed content

error[E0502]: cannot borrow `app` as mutable because it is also borrowed as immutable
  --> src/main.rs:25:44
   |
25 |     println!("{:?} and name is {}", &app, &app.get_name().unwrap())
   |     ---------------------------------------^^^---------------------
   |     |                                |     |
   |     |                                |     mutable borrow occurs here
   |     |                                immutable borrow occurs here
   |     immutable borrow ends here
   |
   = note: this error originates in a macro outside of the current crate
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Looks to me like you want the get_or_insert_with() method. This executes a closure when the Option is None and uses the result as the new value:

fn get_name(&mut self) -> &String {
    self.name.get_or_insert_with(|| String::from("234"))
}

If you already have a value to insert, or creating the value isn't expensive, you can also use the get_or_insert() method:

fn get_name(&mut self) -> &String {
    self.name.get_or_insert(String::from("234"))
}

You'll also need to change your main() function to avoid the borrowing issue. An easy solution would be to derive Clone on your struct and then .clone() it in the call to println!():

fn main() {
    let mut app = App {
        name: None,
        age: 10,
    };

    println!("{:?} and name is {}", app.clone(), app.get_name())
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...