Rust - Lifetime Last Updated : 15 Sep, 2022 Comments Improve Suggest changes Like Article Like Report Lifetimes in Rust refer to the lifetime of how long a variable would live. Lifetimes are associated with references as references cannot live longer than the object from which it is derived. Lifetimes basically describe the scope that a reference is valid for. Lifetimes come into play when we are deallocating an invalid resource Example 1: Rust // Rust program for when we can // determine the variable lifetime // by looking at the code. struct GFG { name: String, id : i32, } fn main() { let x:&GFG { let y: GFG = GFG { name : String::from("GeeksforGeeks"), id:2022, }; } x = &y // allocated reference to a // memory location that is dropped } Output: Here, we can see that since x references y that is deallocated when the scope of Y is finished. Example 2: Rust // Rust-Lifetime program when nothing can // be determined by the lifetime of a variable #![allow(dead_code)] struct GFG { name: String, id : i32, } fn function_name(x: &GFG, y: &GFG)->&GFG { if x.name == "GeeksforGeeks" { x } else { y } } fn main() { let x: GFG = GFG { name : String::from("GeeksForGeeks"), id:2022, }; let y: GFG = GFG { name : String::from("GeeksForGeeks"), id:2022, }; function_name(x,y); } Output: Here, we can see that x and y both are referring to the GFG struct. For this, we have a concept of Lifetime annotation in Rust. Lifetime Annotation:Lifetime Annotation describes the lifetime of a reference variable. Syntax: fn <'x> function_name( param1: &'x type) -> &'x return_value { statement 1; statement 2; ......................... returnValue; } fn: this is the keyword for defining the lifetime of a function notationfunction_name: name of the function'x: lifetime notation&'x: reference variable containing lifetime 'xreturnValue: returns the value of the functionExample 3: Rust #![allow(dead_code)] struct GFG{ name: String, id : i32, } //'g -> lifetime annotation fn function_name<'g> (x: &'g GFG, y: &'g GFG) -> &'g GFG{ if x.name == "GeeksforGeeks" { x } else { y } } fn main(){ let x: GFG = GFG { name : String::from("GeeksForGeeks"), id:2022, }; let y: GFG = GFG { name : String::from("GeeksForGeeks"), id:2022, }; function_name(&x,&y); } Output: Here, we see that the x and y parameters have lifetimes that are the same as that of the function. Hence, the lifetime for both the references is valid too. Hence, the Rust compiler can use the concept borrow checker to check the validity of the reference, and therefore compiler error issues are resolved. Comment More infoAdvertise with us Next Article File I/O in Rust S sayanc170 Follow Improve Article Tags : Rust Similar Reads Rust - Literals A literal is a source code that represents a fixed value and can be represented in the code without the need for computation. The compiler uses by default i32 for integers and f64 for float types/. In Rust, literals are described by adding them in the type as a suffix. Example:  Integer literal 6 h 1 min read Rust lifetime Constructor In Rust, we have a concept of a lifetime constructor that falls under the ownership category. Rust enforces a certain set of rules through its concept of ownership and lifetimes. In this article, we would be learning more about Lifetimes in Rust. In Rust, there are various regions that are present i 3 min read Rust - Aliasing Rust is statically typed and there is a concept of Alias which states that data can be borrowed immutably but during borrowing, the original data cannot be borrowed mutably. Aliasing is useful for the optimization of data. In Aliasing, only one data can be mutably borrowed at a time. On the conditio 2 min read Rust - Interface Rust is a systems programming language with a concept of Interfaces in Rust that is implemented by Traits.Trait in Rust is basically a struct having no bodies for the methods. The logic is generally not defined but only the methods are. Rust enforces us to follow a set of coding practices with imple 3 min read File I/O in Rust File I/O in Rust enables us to read from and write to files on our computer. To write or read or write in a file, we need to import modules (like libraries in C++) that hold the necessary functions. This article focuses on discussing File I/O in Rust. Methods for File I/O Given below are some method 7 min read Rust - Conventions In Rust, there are certain conventions that need to be followed when we are creating dependencies. There are dependencies on libraries in Rust. Cargo! is a dependency manager in Rust that is similar to npm (in JavaScript) and pip (in Python). All the project dependencies are handled by cargo. For in 2 min read Like