23
struct Point {
    x: u32,
    y: u32,
}

struct Line<'a> {
    start: &'a Point,
    end: &'a Point,
}

Here, the only possible option for the start and end fields is to have a lifetime the same or longer than the Line variable that contains them. I can't even imagine how one will go about using a lifetime specifier to say that the fields have a shorter lifespan.

Why do I have to explicitly specify a lifetime here? Is elision not possible in this situation and if so why not?

1
  • 1
    While I suppose it's possible that Rust could choose to elide the lifetimes in a struct definition, it does not do that now. You will only see the term lifetime elision used for functions / methods.
    – Shepmaster
    Jan 5, 2015 at 18:53

2 Answers 2

18

When you define a struct, you aren't making a relation between the lifetime of the struct and the lifetime of the fields. As you point out, the references in the fields have to live longer than the struct.

Instead, what you are doing is providing a "generic lifetime" that will be specialized when you create the struct. This is similar to having a struct with a type parameter:

struct Foo<T>
    foo: T,
}

When you construct the struct, appropriate lifetimes (or types) will be inserted by the compiler, and then it checks that everything still works out.

The other thing is that you can specify the lifetimes with respect to each other:

struct Line<'a, 'b: 'a> {
    start: &'a Point,
    end: &'b Point,
}

This says that start and end can have different lifetimes, so long as the lifetime of end outlives the lifetime of start.

why doesn't the compiler do lifetime elision for structs? It seems in the spirit of Rust to do so

(emphasis mine)

I actually believe that Rust tends towards explicitness, especially when it comes to defining top-level items (like functions, structs).

The rules for lifetime elision for functions have a pretty small scope and were empirically found in RFC 141 to have a high success rate (87%). This was a very good ergonomic return on investment.

Perhaps at some point, similar elision will occur for structs, but it hasn't been a big enough problem yet. If you feel strongly about this, then I'd highly recommend asking for consensus on the user forum, progressing to the developer forum, then ultimately making an RFC.

RFC 2093 adds a small amount of inference. Before it is implemented, you have to express that a generic type as a reference needs to outlive the reference:

struct Foo<'a, T: 'a> {
    start: &'a T,
}

There's no case in which you wouldn't want this bound, so after the RFC is implemented, you can just say:

struct Foo<'a, T> {
    start: &'a T,
}
13
  • 4
    It's not clear from this answer why lifetime is necessary when declaring a struct.
    – RajV
    Jan 5, 2015 at 21:03
  • 1
    @RajV, because there is no obvious way to go without them. Non-static references inside a struct necessarily means that there should be at least one lifetime parameter, but there can be more, and it would mean a different thing. Jan 5, 2015 at 21:27
  • 2
    @Shepmaster, "There is no lifetime elision for struct declarations, although I suppose that the compiler could automatically put 'a for every lifetime'". The question is precisely that: why doesn't the compiler do lifetime elision for structs? It seems in the spirit of Rust to do so, and it seems to me there would be a good reason for this. I don't think your answer addresses that question at all, it only provides examples for explicit lifetime declarations, so I'm very surprised it was accepted. Feb 12, 2015 at 21:38
  • 1
    "This says that start and end can have different lifetimes, so long as the lifetime of end outlives the lifetime of start" - I still do not understand why I, as a the author of the struct, should ever impose such a constraint on the consumers of my API. The only constraint I care about, is that the references remain valid for at least as long as the instance of the struct remains valid, which should be implicit and accomplished without explicit lifetime notation. Why would I ever want any other lifetime constraint beyond that? Apr 22, 2019 at 19:46
  • 2
    @AxiomaticNexus I’d personally stick to one lifetime until something forces me to use multiple. That might change if I was writing a public API that I’d be hesitant to alter after releasing.
    – Shepmaster
    Apr 23, 2019 at 17:11
3

Suppose we have a constructor for Line:

impl<'a> Line<'a> {
    fn new(start: &'a Point, end: &'a Point) -> Line<'a> { // '
        Line {
            start: start,
            end: end,
        }
    }
}

new returns a Line<'a>. To be able to parameterize a type with a lifetime (as we do here with Line<'a>), this type must define lifetime parameters! Although the compiler could automatically define lifetime parameters when necessary, it's much easier to figure out that a type has lifetime parameters (or not) by just looking at its definition in the source code.

Lifetime parameters on structs and enums play an important role in the borrow checker. They let the compiler know that a struct retains borrows to some values. The compiler can then return errors when you try to mutate a value that has active borrows.

fn main() {
    let mut start = Point { x: 2, y: 4 };
    let end = Point { x: 7, y: 10 };
    let line = Line::new(&start, &end);
    start.x = 3; // error: cannot assign to `start.x` because it is borrowed
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.