- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我无法编译我的函数。它总是提示借来的值(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/
我正在使用 tcod-rs。用于绘制到 RootConsole 的每个方法都采用一个可变引用。中央循环是一个 while 循环,它等待窗口关闭、清除屏幕、绘制,然后刷新。 “检查窗口关闭”方法也采用可
This question already has answers here: How to return a reference to a sub-value of a value that is
我是新手,我已经阅读了有关所有权和借用的文档。显然,我开始为这个概念而苦苦挣扎... 这是我的代码: #[derive(Serialize, Deserialize, Debug)] pub stru
我在借阅检查器上遇到了问题。我有一个特征(Physics),它具有getters(例如velocity)和setters(例如velocity_mut)。它还具有使用getter和setter的默认方
我正在用 Rust 编写 Rogue-like。我有一个 Player,它有一个 Vec 的盒装 TimedEffects。定时效果有一个 activate(&mut Player) 和 deacti
我觉得 rc::Weak可以使用(某种)AsRef特征实现。我试图从弱指针借用一些共享内容,但这不会编译: use std::rc::Weak; struct Thing(Weak); impl Th
我正在学习Rust。对于我的第一个程序,我编写了以下代码来维护有关部分排序的数据: use std::collections::{HashMap, HashSet}; struct Node {
这个问题在这里已经有了答案: Cannot borrow as immutable because it is also borrowed as mutable in function argume
在尝试实现一个迭代器以产生对链表元素的可变引用时,我偶然发现了一个奇怪的问题。 这很好用: impl Iterator for LinkedListIterator{ fn next(&mut
我有this minimal example code : use std::borrow::BorrowMut; trait Foo {} struct Bar; impl Foo for Bar
struct State { x: i32 } trait A { fn a(&mut self, b: &i32); } impl A for State { fn a(&m
我有一个 Element 结构,它实现了一个更新方法,该方法需要一个滴答持续时间。该结构包含一个组件向量。允许这些组件在更新时修改元素。我在这里遇到借用错误,我不确定该怎么做。我尝试用 block 修
我正在尝试做这样的事情 use std::cell::{RefCell,Ref,RefMut}; use std::rc::Rc; struct Entity; struct Tile { e
我已连接到 Deribit websocket 以获取选项数据,但以下代码片段仅获得一个有效的数据响应,然后停止更新,但表示它仍在运行。 该网站建议使用 asyncio 模块,但能够使用以下代码片段提
我是一名优秀的程序员,十分优秀!