结构体

定义:

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

创建:

let user1 = User {
    email: String::from("someone@example.com"),
    username: String::from("someusername123"),
    active: true,
    sign_in_count: 1,
};
 
user1.email = String::from("anotheremail@example.com");

根据已有结构体,创建新结构体:

// 注意 user1 的username字段所有权被转移,user1无法使用!
let user2 = User { 
    email: String::from("another@example.com"), 
    ..user1
};

元组结构体(Tuple Struct)

struct Color(i32, i32, i32);
let black = Color(0, 0, 0);