[Answered] What actually **is** 'a?

I've just recently started learning Rust and I'm loving it so far, but I have what is undoubtedly a very simple question I can't find anywhere in the online book or even through searching.

What is the actual meaning/use of 'a ? I have seen it used in a few different places but nowhere seems to actually explain what 'a is!

Is the single-quote used like a keyword, or does 'a have some other unique meaning?

Thanks!

1 Like

See Lifetimes.

2 Likes

For future questions of this nature, there's also the Syntax Index.

2 Likes

Thanks @HaronK and @DanielKeep for their very helpful answers, that's exactly what I was looking for!

You might also like the new book: http://rust-lang.github.io/book/ch10-03-lifetime-syntax.html (this just covers the basics, more advanced stuff will come)

2 Likes

Cool. Here's a suggestion for expanding that:

The thing that's I've not seen specified (i.e. I don't think it is mentioned in the Rust reference at all), is that T: 'lifetime means. That is, what does it mean for a type to be bound by a lifetime? In particular, what does T: 'static mean, exactly? Also what does trait Foo: 'static mean? What is the default lifetime bound for a type? See the following example:

trait T1 {}
trait T2: 'static {}

struct S {}
impl T1 for S {}
impl T2 for S {}

struct SWithRef<'a> {
    r: &'a u8
}
impl<'a> T1 for SWithRef<'a> {}

// Fails because `SWithRef` isn't `'static`.
impl<'a> T2 for SWithRef<'a> {}

fn main() {
    let s = S {};

    // `S: 'static` is inferred.
    let _s_t1: &(T1 + 'static) = &s;
    let _s_t1: &T2 = &s;

    let s_with_ref = SWithRef { r: &1 };
    // `SWithRef` isn't `'static`.
    let _s_t1: &(T1 + 'static) = &s_with_ref;

    // `SWithRef` doresn't implement T2 because it can't.
    let _s_t1: &T2 = &s_with_ref;
}

Note that I did eventually figure this out myself, but I have seen others struggling with this, and I also struggled with it.

1 Like

This is definitely going to go into the bigger chapter, for sure :thumbsup:

It is mentioned in the syntax reference:

  • T: 'a: generic type T must outlive lifetime 'a. When we say that a type 'outlives' the lifetime, we mean that it cannot transitively contain any references with lifetimes shorter than 'a.
  • T : 'static: The generic type T contains no borrowed references other than 'static ones.

It's described thoroughly in RFC 192.

I agree that its lack of discoverability isn't a great thing. Always more to do...

1 Like

First, thanks for pointing those resource to me.

With other languages I've used, I try to skip directly to the language reference to learn things, with the thinking that only the language reference is definitive, and if the language reference doesn't specify something then it is unspecified, even if some other text does claim to specify it. It seems Rust operates on a different model, which isn't bad, but in general it isn't clear which texts are definitive.

Based on your response, now it seems the RFCs are definitive, maybe even more so than the Rust Reference. Maybe language RFCs should be required to include a PR to the Rust Reference that documents, definitively, the change. This way, the Rust Reference would eventually be in sync with the RFCs.

Tricky bit: RFCs can be superseded by other RFCs, and sometimes, parts of RFCs aren't implemented yet.

This is RFC 1636, which was only recently accepted.

1 Like