【问题标题】:Borrowed value does not live long enough - string slice into HashMap借来的值不够长 - 字符串切片到 HashMap
【发布时间】:2016-10-25 20:04:12
【问题描述】:

我无法编译我的函数。它总是抱怨借来的价值活得不够长。我已经用字符串切片切换了 HashMap 的顺序,因为我认为销毁顺序会影响 HashMap 的寿命比放入的字符串切片长一步。但即使在更改之后它仍然不起作用:

代码

fn lyrics_more_bottles(song_template:&mut String, number:i32){
    let mut start_bottles:&str = format!("{} bottles", number).as_str();
    let mut remaining_num:&str = format!("{} bottles", number).as_str();
    let mut template_partials:HashMap<&str, &str> = HashMap::new();

    template_partials.insert("start", start_bottles);
    template_partials.insert("repeat", start_bottles);
    template_partials.insert("remaining", remaining_num);
    template_partials.insert("message", "Take one down and pass it around");

    resolve_template(song_template, template_partials);
}

ERROR_MSG:

lib.rs:45:34: 45:63 error: borrowed value does not live long enough
lib.rs:45     let mut start_bottles:&str = format!("{} bottles", number).as_str();
                                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lib.rs:45:34: 45:63 note: in this expansion of format! (defined in <std macros>)
lib.rs:45:73: 55:2 note: reference must be valid for the block suffix following statement 0 at 45:72...
lib.rs:45     let mut start_bottles:&str = format!("{} bottles", number).as_str();
lib.rs:46     let mut remaining_num:&str = format!("{} bottles", number).as_str();
lib.rs:47     let mut template_partials:HashMap<&str, &str> = HashMap::new();
lib.rs:48 
lib.rs:49     template_partials.insert("start", start_bottles);
lib.rs:50     template_partials.insert("repeat", start_bottles);
          ...
lib.rs:45:5: 45:73 note: ...but borrowed value is only valid for the statement at 45:4
lib.rs:45     let mut start_bottles:&str = format!("{} bottles", number).as_str();
              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lib.rs:45:5: 45:73 help: consider using a `let` binding to increase its lifetime
lib.rs:45     let mut start_bottles:&str = format!("{} bottles", number).as_str();
              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lib.rs:46:34: 46:63 error: borrowed value does not live long enough
lib.rs:46     let mut remaining_num:&str = format!("{} bottles", number).as_str();
                                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lib.rs:46:34: 46:63 note: in this expansion of format! (defined in <std macros>)
lib.rs:46:73: 55:2 note: reference must be valid for the block suffix following statement 1 at 46:72...
lib.rs:46     let mut remaining_num:&str = format!("{} bottles", number).as_str();
lib.rs:47     let mut template_partials:HashMap<&str, &str> = HashMap::new();
lib.rs:48 
lib.rs:49     template_partials.insert("start", start_bottles);
lib.rs:50     template_partials.insert("repeat", start_bottles);
lib.rs:51     template_partials.insert("remaining", remaining_num);
          ...
lib.rs:46:5: 46:73 note: ...but borrowed value is only valid for the statement at 46:4
lib.rs:46     let mut remaining_num:&str = format!("{} bottles", number).as_str();
              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lib.rs:46:5: 46:73 help: consider using a `let` binding to increase its lifetime
lib.rs:46     let mut remaining_num:&str = format!("{} bottles", number).as_str();
              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

【问题讨论】:

    标签: rust lifetime borrow-checker


    【解决方案1】:

    字符串需要一个所有者:

    fn lyrics_more_bottles(song_template:&mut String, number:i32){
        let mut start_bottles = format!("{} bottles", number); // own them as `String`
        let mut remaining_num = format!("{} bottles", number);
        let mut template_partials:HashMap<&str, &str> = HashMap::new();
    
        template_partials.insert("start", &start_bottles); // &String -> &str is implicit
        template_partials.insert("repeat", &start_bottles);
        template_partials.insert("remaining", &remaining_num);
        template_partials.insert("message", "Take one down and pass it around");
    
        resolve_template(song_template, template_partials);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-06
      • 2016-06-10
      • 2020-07-23
      • 1970-01-01
      • 1970-01-01
      • 2014-12-26
      • 2019-11-29
      • 2016-09-20
      相关资源
      最近更新 更多