【发布时间】:2019-04-27 00:18:46
【问题描述】:
考虑以下程序:
fn func(source: &str, state: & Vec<&str>) {
println!("{}", source);
println!("{}", state[0]);
}
fn step<'a>(source: &'a str, state: &mut Vec<&'a str>) {
state.push(&source[4..10]);
func(source, state);
state.push(&source[4..10]);
}
fn main() {
let source = "abcdefghijklmnopqrstuvwxyz";
{
let mut state = Vec::<&str>::new();
step(source, &mut state);
step(source, &mut state);
step(source, &mut state);
}
}
这编译和运行没有错误。我完全可以理解为什么这里需要生命周期说明符 'a,否则你可以运行:
fn main() {
let mut state = Vec::<&str>::new();
{
let source = "abcdefghijklmnopqrstuvwxyz";
step(source, &mut state);
step(source, &mut state);
step(source, &mut state);
}
println!("{}", state[0]);
}
这将导致未定义的输出。我的问题是我想将“func”的两个参数打包到一个结构中,但我无法做到这一点:
struct MyStruct<'a, 'b> {
pub source: &'a str,
pub state: &'b mut Vec<&'b str>,
}
fn func(arg: MyStruct) {
println!("{}", arg.source);
println!("{}", arg.state[0]);
}
fn step<'a>(source: &'a str,
state: &mut Vec<&'a str>) {
state.push(&source[4..10]);
let s = MyStruct{source: source, state: state};
func(s);
state.push(&source[4..10]);
}
fn main() {
let source = "abcdefghijklmnopqrstuvwxyz";
{
let mut state = Vec::<&str>::new();
step(source, &mut state);
step(source, &mut state);
step(source, &mut state);
}
}
上面的代码编译不出来,报错:
107 | state: &mut Vec<&'a str>) {
| ----- consider changing the type of `state` to `&'a mut std::vec::Vec<&'a str>`
108 | state.push(&source[4..10]);
109 | let s = MyStruct{source: source, state: state};
| ^^^^^ lifetime `'a` required
如果我将 state 的状态更改为 &'a mut Vec,则构建失败并显示以下消息:
117 | step(source, &mut state);
| ----- first mutable borrow occurs here
118 | step(source, &mut state);
| ^^^^^ second mutable borrow occurs here
119 | step(source, &mut state);
120 | }
| - first borrow ends here
如果有人能告诉我如何完成这项工作,我将不胜感激。
注意:在这个示例程序中,“source”是一个具有静态生命周期的字符串。在“真实”程序中,“源”没有静态生命周期,而是程序在运行时读取的文件内容。 “源”的寿命与“状态”一样长或更长
【问题讨论】: