gpt4 book ai didi

rust - 借用的值不够长 - 将字符串切片到 HashMap

转载 作者:行者123 更新时间:2023-11-29 08:30:05 29 4
gpt4 key购买 nike

我无法编译我的函数。它总是提示借来的值(value)活得不够长。我已经用字符串切片切换了 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);
}

错误消息:

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();
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

最佳答案

字符串需要一个所有者:

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);
}

关于rust - 借用的值不够长 - 将字符串切片到 HashMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37992440/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com