Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is &'a in Rust Language [duplicate]

Tags:

rust

I read here that

A shared reference type is written &type, or &'a type when you need to specify an explicit lifetime.

Understood & is for shared reference but did not understand what is the difference between type and 'a in Rust language

In another location I read this code:

#[derive(Debug)] struct Person<'a> {     name: &'a str,     age: u8 }  fn main() {     let name = "Peter";     let age = 27;     let peter = Person { name, age };      // Pretty print     println!("{:#?}", peter); } 

What does 'a in the struct Person<'a> { } means? and can I build the same struct using struct Person<'type> { } or struct Person<T> { }?

And what is the meaning of name: &'a str?

And how can I re-code it, if want to avoid using the <'a>

like image 436
Hasan A Yousef Avatar asked Dec 04 '17 19:12

Hasan A Yousef


People also ask

What Is Love female singer?

In 2009, singer and songwriter Diane Birch completely rearranged the song, that later was featured on Billboard magazine's Mashup Monday in 2010.

What is Live Twice?

inspired by Mid-century japanThe newest brand in the Jigger & Pony family, Live Twice welcomes guests from all walks of life, united by their love of cocktails. Whether you're a salaryman or a creative, Live Twice welcomes your most authentic self at the end of the day.

Who is in love what is twice?

Nayeon is Mia from The Princess Diaries, Jeongyeon and Sana are Molly and Sam from Ghost, Mina and Dahyun are Vic and Matthieu from La Boum, Sana and Tzuyu are Mia and Vincent from Pulp Fiction, Jeongyeon and Tzuyu are Romeo and Juliet from Romeo + Juliet, Jihyo and Jeongyeon are Itsuki/Hiroko and male Itsuki from Love ...

Who wrote love twice?

It includes the title track of the same name produced by Park Jin-young. Twice members Jeongyeon, Chaeyoung, and Jihyo also took part in writing lyrics for two songs on the EP. What Is Love?


1 Answers

I found this and this and this and this that explains my question.

The 'a reads ‘the lifetime a’. Technically, every reference has some lifetime associated with it, but the compiler lets you elide (i.e. omit, see "Lifetime Elision") them in common cases.

fn bar<'a>(...) 

A function can have ‘generic parameters’ between the <>s, of which lifetimes are one kind. The <> is used to declare lifetimes. This says that bar has one lifetime, 'a.

Rust has two main types of strings: &str and String. The &str are called ‘string slices’. A string slice has a fixed size, and cannot be mutated. It is a reference to a sequence of UTF-8 bytes.

let greeting = "Hello there."; // greeting: &'static str 

"Hello there." is a string literal and its type is &'static str. A string literal is a string slice that is statically allocated, meaning that it’s saved inside our compiled program, and exists for the entire duration it runs. The greeting binding is a reference to this statically allocated string. Any function expecting a string slice will also accept a string literal.

In the above example

struct Person<'a> {  } 

requires to contain <'a> as the name is defined using:

name: &'a str, 

which is called by:

let name = "Peter"; 

If interested to avoid the usage of 'a then the above code can be re-written as:

#[derive(Debug)] struct Person {    // instead of: struct Person<'a> {     name: String,  // instead of: name: &'a str      age: u8 }  fn main() {     let name = String::from("Peter");  // instead of: let name = "Peter"; which is &'static str     let age = 27;     let peter = Person { name, age };      // Pretty print     println!("{:#?}", peter); } 

As mentioned by @DimitrisSfounis in the comments, in short, "Why is 'a there?" ---- Because the struct definition ties it to a referenced object (in this case, every struct Person instance is referencing a &str) you want to specificly declare an arbitary lifetime and tie these two things together: You want a struct Person instance to only live as long as its referenced object (hence Person<'a> and name: &'a str) so dangling references after each other's death is avoided.

like image 63
Hasan A Yousef Avatar answered Oct 14 '22 11:10

Hasan A Yousef