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
}
posted @   鱼又悲  阅读(132)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类
点击右上角即可分享
微信分享提示