Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is it useful for an associated constant to use a lifetime other than 'static?

RFC 1623, stabilized in Rust 1.17.0, made it so that we do not have to explicitly specify the 'static lifetime in a static or const:

const MY_DEFAULT_NAME: &str = "Anna";
//                      ^ Look, ma! No 'static!

RFC 195 defined associated constants, which were stabilized in Rust 1.20.0:

struct A;

impl A {
    const B: i32 = 42;
}

When trying to combine these two, an error is raised:

struct A;

impl A {
    const MY_DEFAULT_NAME: &str = "Anna";
}
error[E0106]: missing lifetime specifier
 --> src/main.rs:4:28
  |
4 |     const MY_DEFAULT_NAME: &str = "Anna";
  |                            ^ expected lifetime parameter

The related GitHub issue #38831 has a comment:

We decided against it because, in that case, there might be other lifetimes you would want. For example:

trait Foo<'a> {
    const T: &'a str;
}

that said, revisiting this explanation, it feel a bit weak, since the value of an associated constant would still have to be all consisting of static data. So it seems like 'static is a pretty good default if you don't say otherwise.

What is an example of an associated constant with a non-'static lifetime? What benefit does providing a non-'static lifetime bring?

like image 642
Shepmaster Avatar asked Oct 01 '17 19:10

Shepmaster


People also ask

Can a system have more than one time constant?

It should be noted that systems typically have more than one time constant. However, it is very typical for a system to have its time constants far apart from each other, resulting in a separation between fast transient responses and the dominant response (which is the slowest one).

What are the time constants in electrical circuits?

Time constants are everywhere. Since (almost) nothing happens instantaneously, but rather with a delay, processes are said to have time constants associated with them. In electrical systems, purely resistive circuits are static (not dynamic) as there are no energy storing elements. Resistors do not store energy but rather dissipate it.

What happens to a variable when it is declared static?

If a procedure-level variable is declared with the Static keyword, the variable retains its value as long as code is running in any module. When all code has finished running, the variable loses its scope and its value.

Why do we use static classes and static members?

Static classes and static members are useful because they do not require instances created for each new object. That means, they consume fewer resources and no duplication of the same class or member is needed in memory. Static members make code cleaner.


1 Answers

One might consider a constant that is a function:

trait Foo<'a> {
    const BAR: fn(&Self) -> &'a str;
}

struct MyFoo<'a> {
    x: &'a str,
}

impl<'a> Foo<'a> for MyFoo<'a> {
    const BAR: fn(&Self) -> &'a str = my_bar;
}

fn my_bar<'a>(a: &MyFoo<'a>) -> &'a str {
    &a.x
}

Right now, I can't think of how this would be more beneficial than a method.

like image 179
Francis Gagné Avatar answered Sep 21 '22 05:09

Francis Gagné