2015-04-05 49 views
1

的生命周期内有效我得到一生错误,并且无法理解问题是什么。这是造成错误的代码是:引用必须在块的定义为

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) 

什么是与此功能的问题?

回答

4

您在函数中创建q,然后尝试从函数返回对其的引用。由于q在您的函数结束时不再存在,返回值将是一个悬挂引用,这就是编译器不会让您这样做的原因。

btw。你可以看到你的函数签名会出现一些诡计。返回的引用('a)的生命周期实际上并不与任何东西(例如函数参数的生命周期)绑定,这表明这可能不起作用。

+0

如果我将赋值更改为“let q:&'TempDir =&Tempdir ....”它仍然给我那个错误。我如何解决这个问题? – hunterboerner 2015-04-05 17:11:05

+0

是否确定要返回参考?为什么不按值返回'q'? – fjh 2015-04-05 17:13:22

+0

@hunterboerner,你根本不能返回一个对局部变量的引用。如果你这样做,这个变量就会被销毁,你将留下一个悬而未决的参考 - 这正是Rust想要阻止的。在C++中,这样的代码可能会给你一个段错误。 – 2015-04-05 19:13:12

0

我通过直接返回TempDir来解决这个问题。

fn fetch_git_repo(repo_info: &RepoInfo) -> TempDir { 
    let z = TempDir::new("temp_git_clone_dir").unwrap(); 
    let q = z.path().to_str().unwrap().to_string(); 

    println!("{:?}", z.path()); 
    // omitted 

    z 
} 
+0

试过,它的工作原理。谢谢您的帮助。 – hunterboerner 2015-04-06 00:01:56

相关问题