的生命周期内有效我得到一生错误,并且无法理解问题是什么。这是造成错误的代码是:引用必须在块的定义为
fn fetch_git_repo<'a>(repo_info: &RepoInfo) -> &'a TempDir {
let q: TempDir = TempDir::new("temp_git_clone_dir").ok().unwrap();
//let path: &str = dir.path().to_str().unwrap();
let status = Command::new("git").arg("clone").arg(q.path().to_str().unwrap())
.arg(&repo_info.repo_url).status().unwrap_or_else(|e| {
panic!("Failed to run git clone: {}", e)
});
if !status.success() {
panic!("Git clone failed!");
}
&q
}
和错误本身是:
test.rs:88:6: 88:7 error: `q` does not live long enough
test.rs:88 &q
^
test.rs:75:60: 89:2 note: reference must be valid for the lifetime 'a as defined on the block at 75:59...
test.rs:75 fn fetch_git_repo<'a>(repo_info: &RepoInfo) -> &'a TempDir {
test.rs:76 let q: TempDir = TempDir::new("temp_git_clone_dir").ok().unwrap();
test.rs:77 //let path: &str = dir.path().to_str().unwrap();
test.rs:78
test.rs:79 let status = Command::new("git").arg("clone").arg("")
test.rs:80 .arg(&repo_info.repo_url).status().unwrap_or_else(|e| {
...
test.rs:76:70: 89:2 note: ...but borrowed value is only valid for the block suffix following statement 0 at 76:69
test.rs:76 let q: TempDir = TempDir::new("temp_git_clone_dir").ok().unwrap();
test.rs:77 //let path: &str = dir.path().to_str().unwrap();
test.rs:78
test.rs:79 let status = Command::new("git").arg("clone").arg("")
test.rs:80 .arg(&repo_info.repo_url).status().unwrap_or_else(|e| {
test.rs:81 panic!("Failed to run git clone: {}", e)
什么是与此功能的问题?
如果我将赋值更改为“let q:&'TempDir =&Tempdir ....”它仍然给我那个错误。我如何解决这个问题? – hunterboerner 2015-04-05 17:11:05
是否确定要返回参考?为什么不按值返回'q'? – fjh 2015-04-05 17:13:22
@hunterboerner,你根本不能返回一个对局部变量的引用。如果你这样做,这个变量就会被销毁,你将留下一个悬而未决的参考 - 这正是Rust想要阻止的。在C++中,这样的代码可能会给你一个段错误。 – 2015-04-05 19:13:12