免责声明:学习Rust。
我具有以下功能:
fn log_action(log_file: &String, transferred_files_count: &i32) -> Result<()> {
let mut lines = Vec::new();
let count_as_string = transferred_files_count.to_string();
lines.push(&count_as_string); // error
append_lines_to_file(log_file, &lines)?;
Ok(())
}
当尝试将&count_as_string
推入向量时,出现错误borrowed value does not live long enough
。偶然地,我注意到当我交换两行顶行时,错误消失了:
fn log_action(log_file: &String, transferred_files_count: &i32) -> Result<()> {
let count_as_string = transferred_files_count.to_string();
let mut lines = Vec::new();
lines.push(&count_as_string); // no error!
append_lines_to_file(log_file, &lines)?;
Ok(())
}
我很困惑,我认为这两行的顺序应该无关紧要。有人可以在这里解释逻辑吗?