Lifetime Management in Rust Structs

RustRustBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we have some Rust code that demonstrates the usage of lifetimes in structs. The code includes a struct called Borrowed that holds a reference to an i32, and the reference must outlive the struct itself. There is also a struct called NamedBorrowed with two references to i32, both of which must outlive the struct. Additionally, there is an enum called Either that can either be an i32 or a reference to one, and the reference must outlive the enum. Finally, the code creates instances of these structs and enum, and prints their contents to showcase the usage of lifetimes in Rust.

Note: If the lab does not specify a file name, you can use any file name you want. For example, you can use main.rs, compile and run it with rustc main.rs && ./main.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL rust(("`Rust`")) -.-> rust/BasicConceptsGroup(["`Basic Concepts`"]) rust(("`Rust`")) -.-> rust/DataTypesGroup(["`Data Types`"]) rust(("`Rust`")) -.-> rust/FunctionsandClosuresGroup(["`Functions and Closures`"]) rust(("`Rust`")) -.-> rust/DataStructuresandEnumsGroup(["`Data Structures and Enums`"]) rust/BasicConceptsGroup -.-> rust/variable_declarations("`Variable Declarations`") rust/DataTypesGroup -.-> rust/integer_types("`Integer Types`") rust/FunctionsandClosuresGroup -.-> rust/function_syntax("`Function Syntax`") rust/FunctionsandClosuresGroup -.-> rust/expressions_statements("`Expressions and Statements`") rust/DataStructuresandEnumsGroup -.-> rust/method_syntax("`Method Syntax`") subgraph Lab Skills rust/variable_declarations -.-> lab-99207{{"`Lifetime Management in Rust Structs`"}} rust/integer_types -.-> lab-99207{{"`Lifetime Management in Rust Structs`"}} rust/function_syntax -.-> lab-99207{{"`Lifetime Management in Rust Structs`"}} rust/expressions_statements -.-> lab-99207{{"`Lifetime Management in Rust Structs`"}} rust/method_syntax -.-> lab-99207{{"`Lifetime Management in Rust Structs`"}} end

Structs

Annotation of lifetimes in structures are also similar to functions:

// A type `Borrowed` which houses a reference to an
// `i32`. The reference to `i32` must outlive `Borrowed`.
#[derive(Debug)]
struct Borrowed<'a>(&'a i32);

// Similarly, both references here must outlive this structure.
#[derive(Debug)]
struct NamedBorrowed<'a> {
    x: &'a i32,
    y: &'a i32,
}

// An enum which is either an `i32` or a reference to one.
#[derive(Debug)]
enum Either<'a> {
    Num(i32),
    Ref(&'a i32),
}

fn main() {
    let x = 18;
    let y = 15;

    let single = Borrowed(&x);
    let double = NamedBorrowed { x: &x, y: &y };
    let reference = Either::Ref(&x);
    let number    = Either::Num(y);

    println!("x is borrowed in {:?}", single);
    println!("x and y are borrowed in {:?}", double);
    println!("x is borrowed in {:?}", reference);
    println!("y is *not* borrowed in {:?}", number);
}

Summary

Congratulations! You have completed the Structs lab. You can practice more labs in LabEx to improve your skills.

Other Rust Tutorials you may like