Learn Rust Language : how to code a struct in Rust

Define a struct, think of it like a Python dictionary, there is an option to include methods as well but we’ll leave that for another day.

Note : ” If the struct also contains heap based data types such as a STRING, then the STRING pointer and associated information are put on the stack with the rest of the struct while the contents of the string lives on the heap.

The most basic of structs in Rust
/* Rust learning : Day 3 */

// Create struct to store student details
struct Student {
    name: String,
    age: u8,
}

fn main() {
    let student_bob = Student {
        name: String::from("bob"),
        age: 23,
    };

    println!("{} {}", student_bob.name, student_bob.age);
}

Try the Rust Playground here :

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=8226aa6960106b338ccf2747ae2aa81e

View the Gist :

https://gist.github.com/rust-play/8226aa6960106b338ccf2747ae2aa81e

YouTube Rust Language Playlist:

Check out my “Learning rust for beginners” playlist on YouTube