Day 8: Structs
EasyStructsDataTypes

Day 8: Structs

Structs let you create custom types by grouping related data together.

Defining a Struct

struct User {
    username: String,
    email: String,
    active: bool,
    sign_in_count: u64,
}

Creating Instances

let user = User {
    username: String::from("alice"),
    email: String::from("alice@example.com"),
    active: true,
    sign_in_count: 1,
};

Field Init Shorthand

When variable names match field names:

fn create_user(username: String, email: String) -> User {
    User {
        username,  // same as username: username
        email,     // same as email: email
        active: true,
        sign_in_count: 1,
    }
}

In Solana, program accounts are modeled as structs with careful memory layouts!

The Task

  1. Define a User struct with name: String and age: u32
  2. Implement create_user to create a User instance
  3. Implement greet to return a greeting string

Requirements

  • Struct must have fields name and age
  • greet returns: "Hi, I'm {name} and I'm {age} years old"
  • Test creates User("Alice", 30)

Hints

  • Use String::from(name) or name.to_string() to convert &str to String
  • Use format!() macro to build the greeting string:
    format!("Hi, I'm {} and I'm {} years old", user.name, user.age)
Language: Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Output
Run to see the result here.
    Day 8: Structs · RUST Challenge | learn.sol