5/5
# Struct ## Example Execute the following command to run [`./solutions/examples/struct.rs`](https://github.com/Cyfrin/rust-crash-course/blob/main/topics/struct_type/solutions/examples/struct.rs) ```shell cargo run --example struct ``` ## Exercises Exercises are in [`./exercises/src/lib.rs`](https://github.com/Cyfrin/rust-crash-course/blob/main/topics/struct_type/exercises/src/lib.rs) ### Exercise 1 ```rust #[derive(Debug)] pub struct Account {} ``` Create an `struct` named `Account` having the fields - `address` of the type `String` - `balance` of the type `u32` Prefix all struct fields with the `pub` keyword. ### Exercise 2 ```rust pub fn new(address: String) -> Account { todo!(); } ``` Create a function that returns the `Account` struct with `address` from the input, and `balance` set to 0. ## Test ```shell cargo test ```
Execute the following command to run ./solutions/examples/struct.rs
Exercises are in ./exercises/src/lib.rs
Create an struct
named Account
having the fields
address
of the type String
balance
of the type u32
Prefix all struct fields with the pub
keyword.
Create a function that returns the Account
struct with address
from the input, and balance
set to 0.
A foundational introduction to Structs - Learn to define custom data types in Rust by creating an `Account` `struct` with public `address` (`String`) and `balance` (`u32`) fields. You'll also implement an associated `new` function to initialize `Account` instances with a given address and a default balance of zero.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
Introduction to the Rust programming language
Rust variables and functions
Scalar types, arrays, strings, enum, structs, vectors, and hash maps in Rust
Rust control flows: If / else, if let and let else, loop, match
Rust ownership, including borrow and references
Rust error handling
Rust Modules
Rust Traits
Last updated on July 11, 2025
Rust Developer
Rust Programming BasicsDuration: 6min
Duration: 18min
Duration: 47min
Duration: 15min
Duration: 19min
Duration: 8min
Duration: 12min
Duration: 46min
Duration: 14min
Course Overview
About the course
Introduction to the Rust programming language
Rust variables and functions
Scalar types, arrays, strings, enum, structs, vectors, and hash maps in Rust
Rust control flows: If / else, if let and let else, loop, match
Rust ownership, including borrow and references
Rust error handling
Rust Modules
Rust Traits
Last updated on July 11, 2025