Hacker News new | past | comments | ask | show | jobs | submit login

cool post. what about traits and 'static?

Anyhow; Its definitely worth making two points about lifetimes very clear:

1) They don't exist at runtime.

Lifetimes are purely a compile time thing the compiler uses for formal safety.

2) 'a is not a actual lifetime, it is the minimum possible life time valid for that item; ie. a bound, not a concrete value.

&'a means; this arg/trait/ref/whatever must live at least as long as 'a. That's why different objects with different lifetimes can coexist in a struct of 'a, without needing 'a, 'b, 'c etc for each reference in the struct.




It's not clear from context in your comment if you're saying this, but, for `x: &'a T`, 'a is a bound in two ways:

- it is the maximum possible lifetime of the x variable itself,

- it is the minimum lifetime of the `T` instance x points to.

These are 100% related of course (x can't point to something that is invalid), but it is worth being careful about which end of a reference/pointer you're talking about when talking about validity.


Thanks for this comment, that clarifies things a lot for me. I think it should be in the docs (with equal brevity) if not already.


I've made a note to check it out, thanks.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: