3 ms·
I wish there was a way to escape lifetime annotation propagation in Rust. What I mean is, if you have a struct with an owned field that you refactor to a borrow
by andrenth 8y ago
I wish there was a way to escape lifetime annotation propagation in Rust. What I mean is, if you have a struct with an owned field that you refactor to a borrow, you have to add <‘a> everywhere that struct name appears in your program.
- beatgammit 8y agoI'm hopeful that the work on non-lexical lifetimes will extend to this in some fashion, but honestly I'm not sure it's possible while making the same guarantees since you need to make sure that the borrow expires before the data becomes invalid. However, in many cases, you probably just want a Box or something instead of a borrow within a struct. Do you have a specific example where you want a borrow?
- andrenth 8y agoWell let’s say you read command line args into a Vec<String> and then you want to use each in some Foo struct, but you want to avoid clones, so you make the field a &str. Now your Foo must become Foo<‘a> everywhere. I understand the guarantees, it’s just the syntactic overhead that annoys me.
- steveklabnik 8y agoAt most, you’d have to propagate a <‘_>; the lifetime is part of the type and showing it matters for the same reason showing any other type parameter matters.