Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to implement a builder similar to the debug builders defined by the standard library. They are defined using structures like the following:

struct DebugFoo<'a, 'b: 'a> {
    fmt: &'a mut std::fmt::Formatter<'b>
}

Since I don't understand what the form <'a, 'b: 'a> means nor I can find it mentioned in the Rust book or the Rust reference (at least concerning lifetimes), I just tried to remove what I don't understand to see what happens:

struct DebugFoo<'a, 'b> {
    fmt: &'a mut std::fmt::Formatter<'b>
}

Compiling it I get this error:

in type `&'a mut core::fmt::Formatter<'b>`, reference has a longer 
lifetime than the data it references

And this note:

the pointer is valid for the lifetime 'a as defined on the struct at 1:0
but the referenced data is only valid for the lifetime 'b as defined on
the struct at 1:0

It makes sense to me: 'a and 'b are different lifetimes so, to be on the safe side, Rust (the borrow checker?) assumes that 'a will outlive 'b, and throws the error.

Now I can guess that <'a, 'b: 'a> means that the lifetime 'b must be longer than the lifetime 'a. I've guessed right? Or there is more? How can I find it documented?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
886 views
Welcome To Ask or Share your Answers For Others

1 Answer

The colon is read "outlives", so

'long: 'short

is read "'long outlives 'short".

As for an official doc on the topic, the only place I've seen it documented so far is in the RFC on lifetime bounds.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...