構造体
2022/1/1 16:00:00
fn main() {
struct Color(i32, i32, i32); //タプル構造体
let black = Color(0, 0, 0);
println!("{}", black.0); //0
}
多くは前の個体の値を使用しつつ、変更する箇所もある形で新しい個体を生成できる記法
struct User {
username: String,
email: String,
sign_in_count: u64,
active: bool,
}
fn main() {
let user1 = User {
username: String::from("username1"),
email: String::from("[email protected]"),
active: true,
sign_in_count: 1,
};
let user2 = User {
username: String::from("username2"),
email: String::from("[email protected]"),
..user1 //構造体更新記法でuser1から残りの欄の値を引き継ぐ
};
println!("{}", user2.username); //username2
}
Rustの構造体
struct User {
username: String,
email: String,
sign_in_count: u64,
active: bool,
}
fn main() {
let user1 = User {
email: String::from("[email protected]"),
username: String::from("someusername123"),
active: true,
sign_in_count: 1,
};
println!("{}", user1.email); //[email protected]
}