Back
Day 8: Structs
EasyStructsData8 / 30

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)
LanguageRust
Loading editor...
Run cases to compare your output against each configured input. Submit saves progress only when every case passes.
    Day 8: Structs · RUST Exercise | learn.sol