Does rust life really only apply to links?

I'm trying to wrap my head around the life of Rust (since the official guides don't explain them very well).

Do rust time things only refer to references, or can they refer to base / primitive values ​​as well?

+3


source to share


4 answers


Lifetimes is a relationship between values ​​and references to specified values.

To understand this link, I will use broken parallel: houses and addresses.

House is an individual. It is being built on a piece of land at some time, will live for several tens or hundreds of years, can be renovated several times during this time, and will most likely be destroyed at some point.

The address is a logical object, it can point to a house or other physical objects (field, school, train station, company headquarters, ...).


The lifespan of a house is relatively transparent: it represents the length of time a house can be used, from its creation to the time it is destroyed. During this time, the house can undergo several repairs, and what used to be a simple boar can become a full-fledged estate, but this does not concern us; for our purpose, the house lives in all these transformations. Only its creation and final destruction matter ... although it would be better if no one was in the bedroom when we tore the roof off.

Now imagine that you are a real estate agent. You don't keep the houses you sell in your office, this is impractical; you still keep your addresses!

Without a clue of life, from time to time your clients will complain because the address you sent them was the address of the garbage dump, and not at all this lovely two-story house you photographed in.You may also get a couple of inquiries from the police station about why people holding onto the brochure from your office were found in just a destroyed house, a subsequent lawsuit could shut down your business.



This is obviously a risk to your business and therefore you should look for the best solution. What if each address can be tagged with the lifetime of the house it links to so you don't know to send people to death (or disappointment)?


You may have learned a manual memory management strategy in this garbage dump; in C it is for you, the real estate agent developer , so that your addresses (pointers / links) always refer to residential buildings.

In Rust, however, the links marked with a special marker: 'enough

; it represents the lower limit of the lifetime of the mentioned value.

When the compiler checks if your use of a link is safe or not, it asks the question:

Is the value preserved?

It doesn't matter if this value will last for 100 years after that, as long as it lives long 'enough

to use it.

+4


source


Yes, they only refer to references, however, these references can be of primitive types. Rust is not like Java (and similar languages), which distinguishes between primitive types, which are passed by value, and more complex types (objects in Java), which are passed by reference. Complex types can be allocated on the stack and passed by value, and references can be applied to primitive types.

For example, here's a function that takes two references to i32

's and returns a reference to the larger one:



fn bigger<'a>(a: &'a i32, b: &'a i32) -> &'a i32 {
    if a > b { a } else { b }
}

      

It uses the lifetime 'a

to communicate that the lifetime of the returned link is the same as the links passed to.

+1


source


No, they also refer to values. If it is not clear from the context how long they will live, they should also be annotated. It is then called the life boundary.

In the following example, you must specify that the value referenced by the link lives at least up to the link itself:

use std::num::Primitive;

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

      

Try uninstalling + 'a

and the compiler will complain. This is necessary because it T

can be implemented Primitive

.

+1


source


When you see an annotation for life (for example 'a

) in code, a reference or a borrowed pointer is almost always used .

Complete syntax for borrowed pointers &'a T

. 'a

- referent lifetime. T

- type of referent.

Structures and enumerations can have lifetime parameters. This usually results from a structure or enumeration containing a borrowed pointer. When you store a borrowed pointer in a structure or enum, you must explicitly specify the lifetime of the referent. For example, an Cow

enumeration in the standard library contains a borrowed pointer in one of its variants. Therefore, it has a lifetime parameter that is used in the borrowed pointer type to determine the lifetime of the referent.

Traits can have type constraints as well as life binding. The lifetime graph indicates the largest area in which all borrowed pointers in a particular implementation of this feature are valid (ie, their referents are alive). If the implementation contains no borrowed pointers, then the lifetime is defined as 'static

. Lifetime bounds can be displayed in type parameter definitions, clauses, where

and feature objects.

Sometimes you might want to define a struct or enum with a lifetime parameter, but no corresponding value to borrow. You can use the marker type, for example ContravariantLifetime<'a>

, to ensure that the lifetime parameter has the correct variance ( ContravariantLifetime

matches the variance from borrowed pointers, no marker, the lifetime will be bivariant, which means the lifetime can be replaced with any other lifetime ... not very useful!). See an example of this use case here.

0


source







All Articles