其他分享
首页 > 其他分享> > Rust定义并实例化结构体

Rust定义并实例化结构体

作者:互联网

Rust 中使用 struct 关键字定义并实例化结构体。

示例程序:

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

fn main() {
    let user1 = User {
        username: String::from("yuyoubei"),
        email: String::from("yuyoubei@qq.com"),
        actice: true,
        sign_in_count: 123,
    };

    println!("{}, {}, {}, {}", user1.username, user1.email, user1.actice, user1.sign_in_count); // 输出 yuyoubei, yuyoubei@qq.com, true, 123
}

标签:username,yuyoubei,String,user1,sign,实例,email,Rust,定义
来源: https://www.cnblogs.com/yuyoubei/p/15855225.html