capypad
0 day streak
rust / beginner
Snippet

Defining and Instantiating Structs

Structs allow you to create custom data types by grouping related values together. Each field has a name and a type.

snippet.rs
rust
1
2
3
4
5
6
7
8
9
10
11
12
13
struct User {
username: String,
email: String,
active: bool,
}
 
fn main() {
let user1 = User {
email: String::from("[email protected]"),
username: String::from("someuser123"),
active: true,
};
}
Breakdown
1
struct User { ... }
Defines the structure of the custom type with field names and types.
2
let user1 = User { ... }
Creates an instance by assigning values to all defined fields.