Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rust ';静态寿命来源于_Rust - Fatal编程技术网

Rust ';静态寿命来源于

Rust ';静态寿命来源于,rust,Rust,我得到了以下代码(没有多大意义,只是一个最小化的测试用例): 这里发生了很多事情。为了解释这个问题,让我们来看看这个问题的更简化版本。为了避免隐藏东西,我还替换了try!()及其属性 这里我们看到了另一个关键点:type框您能将完整的编译器输出添加到您的问题中吗?这其实很重要,不仅仅是噪音:)@LukasKalbertodt附后。如果你想玩这个游戏,这个代码是完整的。谢谢,这很有道理。最后,我在rust()中把这个案例作为一个“这个错误并没有真正帮助”的问题发布了出来。Lukas回答得很好。我知

我得到了以下代码(没有多大意义,只是一个最小化的测试用例):


这里发生了很多事情。为了解释这个问题,让我们来看看这个问题的更简化版本。为了避免隐藏东西,我还替换了
try!()
及其属性


这里我们看到了另一个关键点:type
框您能将完整的编译器输出添加到您的问题中吗?这其实很重要,不仅仅是噪音:)@LukasKalbertodt附后。如果你想玩这个游戏,这个代码是完整的。谢谢,这很有道理。最后,我在rust()中把这个案例作为一个“这个错误并没有真正帮助”的问题发布了出来。Lukas回答得很好。我知道我们不应该写这样的评论但是。。这确实是一个很好的答案。所以(拇指)。
extern crate rustc_serialize;

use rustc_serialize::json::Json;
use std::error::Error;

struct SomeStruct;

#[derive(Debug)]
enum SomeError<'a> {
    Something(&'a str),
    Other,
}

fn do_stuff(doc: &Json) -> Result<SomeStruct, SomeError> {
    Ok(SomeStruct)
}

fn get_things(doc: &Vec<Json>) -> Result<SomeStruct, Box<Error>> {
    let res = try!(doc.get(0).ok_or(SomeError::Other));
    Ok(try!(do_stuff(&res)))                             //// line 20
}

fn main() {
    let _ = get_things(&vec!(Json::Null));
}

impl<'a> std::fmt::Display for SomeError<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        write!(f, "blah")
    }
}
impl<'a> Error for SomeError<'a> {
    fn description(&self) -> &str { "blah" }
}
error: cannot infer an appropriate lifetime for lifetime parameter in function call due to conflicting requirements [E0495]
  --> src/main.rs:19:24
19 |>     let res = try!(doc.get(0).ok_or(SomeError::Other));
   |>                        ^^^
src/main.rs:19:15: 19:55: note: in this expansion of try! (defined in <std macros>)
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the block at 18:65...
  --> src/main.rs:18:66
18 |> fn get_things(doc: &Vec<Json>) -> Result<SomeStruct, Box<Error>> {
   |>                                                                  ^
note: ...so that reference does not outlive borrowed content
  --> src/main.rs:19:20
19 |>     let res = try!(doc.get(0).ok_or(SomeError::Other));
   |>                    ^^^
src/main.rs:19:15: 19:55: note: in this expansion of try! (defined in <std macros>)
note: but, the lifetime must be valid for the static lifetime...
note: ...so that types are compatible (expected std::result::Result<SomeStruct, Box<std::error::Error + 'static>>, found std::result::Result<SomeStruct, Box<std::error::Error>>)
 --> <std macros>:5:8
5 |> return $ crate :: result :: Result :: Err (
  |>        ^
src/main.rs:20:8: 20:28: note: in this expansion of try! (defined in <std macros>)
enum SomeError<'a> {
    Something(&'a str),
    Other,
}

// ... impls Debug, Display, Error for SomeError ...

fn do_stuff(doc: &u32) -> Result<(), SomeError> { Ok(()) }

fn get_things(doc: &Vec<u32>) -> Result<(), Box<Error>> {
    match do_stuff(&v[0]) {   // `try!` expands into this match 
        Ok(v) => Ok(v),
        Err(e) => Err(e.into()),
    }  //             ^^^^^^^^--- #1

}

fn main() {
    let _ = get_things(&vec![]);
}
impl<'a, E: Error + 'a> From<E> for Box<Error + 'a>
fn get_things<'a>(doc: &'a Vec<u32>) -> Result<(), Box<Error + 'a>> {
    ...
}