Episode of life

Can someone explain what life deprivation is in very simple terms?

From rust documents, "Rust" supports a powerful local type of output in function bodies, but not in their element signatures. It is forbidden to talk about types based on only one element signature. However, for ergonomic reasons, a very limited secondary derivation algorithm, called a "lifetime exception", is used in estimating life expectancy. Life cycle evolution is concerned solely with determining the parameters of life using three easily remembered and unambiguous rules. This means that a lifelong exception acts as a shorthand for writing an elementโ€™s signature, without hiding the actual types used as full local output, will be applied to it. "

I do not understand what it means. What are element captions? What do lifetime values โ€‹โ€‹mean? Some examples or analogy would be helpful. Thanks in advance for taking the time to help me.

+4
source share
1 answer

Firstly, the signature of an element is just a bit that gives the name and types of your function, that is, everything you need to call (without having to know how it is implemented); eg:

fn foo(x: u32) -> u32;

Here is another that accepts the link &str:

fn bar<'a>(s: &'a str) -> &'a str;

Rust, ; . bar , " ". : " , , . Rust.

, , , Rust " elision" (.. " " ). , () , Rust . , "" .

, :

  • , , . , :
fn f(x: &T, y: &U)

:

fn f<'a, 'b>(x: &'a T, y: &'b U)

.. .

  1. , . :
struct U<'a> {}  // struct with a lifetime parameter

fn f(x: &T) -> &U

:

fn f<'a>(x: &'a T) -> &'a U<'a>
  1. , , - &self &mut self (.. ), , self. , . , :
impl S {
    fn get_my_item(&self, key: &str) -> &str {}
}

:

fn get_my_item<'a,'b>(&'a self, key: &'b str) -> &'a str  // use the self lifetime

.

+12

Source: https://habr.com/ru/post/1659306/


All Articles