Woher stammt die 'statische Lebensdauer von

Ich habe den folgenden Code (macht nicht viel Sinn, nur einen minimierten Testfall):

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" }
}

Dies schlägt fehl, wenn in Zeile 20 ein Typ nicht übereinstimmt:expected std::result::Result<SomeStruct, Box<std::error::Error + 'static>>, found std::result::Result<SomeStruct, Box<std::error::Error>>

Ich verstehe nicht, woher das'static lebenslange Anforderung kommen plötzlich von. Wenn ich die Aufzählung in @ ändeSomething(&'static str) es funktioniert einwandfrei, aber warum kann ich hier keinen weniger einschränkenden Wert verwenden?

Der Fehler erwähnt, dassdoc ist der geliehene Inhalt, den der Fehler nicht überleben kann ... aber es scheint keine Beziehung zwischen diesen beiden Typen zu geben.

Full error:

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>)

Antworten auf die Frage(2)

Ihre Antwort auf die Frage