我的借阅检查员喝醉了吗?

时间:2015-01-19 18:21:51

标签: rust

  

参考必须在50:74对该区块有效...   ...但借来的价值仅对50:74的区块有效

右。那有什么问题?

有问题的街区:

pub fn git_upload_pack(self: &mut GitConnect) -> Result<String, &str> {
    let c = format!("git-upload-pack {}\0host={}\0", self.repo.path, self.repo.domain);

    let mut out = String::new();
    let data = try!(self.command(c.as_slice()));

    for line in data.iter() {
        out.push_str(from_utf8(line.as_slice()).unwrap());
    }

    Ok(out)
}

self.command

fn command(self: &mut GitConnect, command: &str) -> Result<Vec<Vec<u8>>, &str> {

完整错误:

src/protocol/git_connect.rs:54:38: 54:39 error: `c` does not live long enough
src/protocol/git_connect.rs:54         let data = try!(self.command(c.as_slice()));
                                                                    ^
<std macros>:1:1: 6:60 note: in expansion of try!
src/protocol/git_connect.rs:54:20: 54:53 note: expansion site
src/protocol/git_connect.rs:50:75: 61:6 note: reference must be valid for the anonymous lifetime #1 defined on the block at 50:74...
src/protocol/git_connect.rs:50     pub fn git_upload_pack(self: &mut GitConnect) -> Result<String, &str> {
src/protocol/git_connect.rs:51         let c = format!("git-upload-pack {}\0host={}\0", self.repo.path, self.repo.domain);
src/protocol/git_connect.rs:52 
src/protocol/git_connect.rs:53         let mut out = String::new();
src/protocol/git_connect.rs:54         let data = try!(self.command(c.as_slice()));
src/protocol/git_connect.rs:55 
                               ...
src/protocol/git_connect.rs:50:75: 61:6 note: ...but borrowed value is only valid for the block at 50:74
src/protocol/git_connect.rs:50     pub fn git_upload_pack(self: &mut GitConnect) -> Result<String, &str> {
src/protocol/git_connect.rs:51         let c = format!("git-upload-pack {}\0host={}\0", self.repo.path, self.repo.domain);
src/protocol/git_connect.rs:52 
src/protocol/git_connect.rs:53         let mut out = String::new();
src/protocol/git_connect.rs:54         let data = try!(self.command(c.as_slice()));

1 个答案:

答案 0 :(得分:3)

对我来说这看起来像个错误。

此签名:

fn command(self: &mut GitConnect, command: &str) -> Result<Vec<Vec<u8>>, &str>

根据lifetime elision rules,应该相当于这个:

fn command<'a, 'b>(self: &'a mut GitConnect, command: &'b str) -> Result<Vec<Vec<u8>>, &'a str>

事实上,如果你重写你的command()以使用这个扩展的变体,它应该编译。此外,如果您使用简写self参数定义:

fn command(&mut self, command: &str) -> Result<Vec<Vec<u8>>, &str>

然后它也会编译。

目前似乎

fn command(self: &mut GitConnect, command: &str) -> Result<Vec<Vec<u8>>, &str>

相当于

fn command<'a>(self: &'a mut GitConnect, command: &'a str) -> Result<Vec<Vec<u8>>, &'a str>

由于存在生命周期错误而给出了完全相同的错误:command参数的生命周期被声明为与self相同,因此它赢得了&#39;使用局部变量,其生命周期将短于self