4 ms·
One of the examples in the paper has some code that you can't actually write in Rust, it's close to this: let msg: &'msg str = "Hello World"; You can writ
by bluejekyll 8y ago
One of the examples in the paper has some code that you can't actually write in Rust, it's close to this:
let msg: &'msg str = "Hello World";
You can write something similar and allow the compiler to infer the lifetime:
let msg: &'_ str = "Hello World";
I often find myself declaring the type when I get into confusing type issues in Rust, but you can't do that as easily with lifetimes. With non-lexical lifetimes this has been less of an issue, but still there are times when I would like to be able to declare the lifetime like the first example.
This error example shows the valid uses of lifetimes: https://doc.rust-lang.org/nightly/error-index.html#E0261 https://doc.rust-lang.org/nightly/error-index.html#E0261
- webkike 8y agoI believe you can actually name the lifetime of a block, via the same mechanism that makes labeled breaks and continues possible.
- bluejekyll 8y agoDo you have an example of what you mean?
- coolreader18 8y agoLike this: 'a: { let a_var: u32 = 42; let a_ref: &'a u32 = &a_var; } // ^^^^^ only valid in 'a: {}
- gpm 8y agoThat doesn't compile with "use of undeclared lifetime name `'a`"
- steveklabnik 8y agoYou can name loop labels like this, but you cannot use them to introduce actual lifetimes, that’s right.
- Retra 8y agoI think that was just a proposal that never got accepted? Never see it in real code either way.