«

Rust - 结构体

ljierui 发布于 阅读:100 技术杂谈


1、结构体

1.1、结构体的定义

// 定义struct
struct User{
    username:String,
    email:String,
    sign_in_count:u64,
    active:bool,
}
fn main() {
    println!("Hello, world!");
}

1.2、实例化结构体

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

fn main() {
    println!("Hello, world!");

    // 定义了几个属性,就要实例化几个
    let ren = User{
        username:String::from("小明"),
        email:String::from("admin@gmail"),
        sign_in_count:19,
        active:true,
    };
}

1.3、获取结构体中的字段

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

fn main() {
    println!("Hello, world!");

    // 定义了几个属性,就要实例化几个
    let mut ren = User{
        username:String::from("小明"),
        email:String::from("admin@gmail"),
        sign_in_count:19,
        active:true,
    };

    // 通过点访问属性
    ren.username = String::from("小红");
}

1.4、Struct作为函数的返回值

// 定义 User 结构体
struct User {
    username: String,
    email: String,
    sign_in_count: u64,
    active: bool,
}

// 定义 build_user 函数,它接受两个字符串并返回一个 User 实例
fn build_user(email: String, username: String) -> User {
    User {
        username, // 等同于 username: username
        email,    // 等同于 email: email
        sign_in_count: 1,
        active: true, // 使用具体的值 true 来初始化布尔类型
    }
}

1.5、Struct更新语法

#[derive(Debug)]
struct User {
    username: String,
    email: String,
    sign_in_count: u64,
    active: bool,
}

fn create_user_with_new_email(user: &User, new_email: String) -> User {
    User {
        email: new_email, // 提供一个新的 email 字段值
        ..*user // 其他字段从 user 参数中的现有实例复制
    }
}

fn main() {
    let user1 = User {
        username: String::from("user123"),
        email: String::from("[email protected]"),
        sign_in_count: 1,
        active: true,
    };

    let user2 = create_user_with_new_email(&user1, String::from("[email protected]"));

    println!("User1: {:?}", user1);
    println!("User2: {:?}", user2); // user2 将具有与 user1 相同的字段除了 email
}

1.6、Tuple struct

struct Color(i32,i32,i32);
struct Point(i32,i32,i32);

fn main(){
    let black = Color(0,0,0);
    let origin = Point(0,0,0);
}

1.7、Unit-Like Struct(空结构体)

1.8、struct数据的所有权

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

1.9、Struct的方法

#[derive(Debug)]
struct Rectangle{
    width : u32,
    length : u32,
}
// 定义方法
impl Rectangle {
    fn area(&self) -> u32{
        self.width * self.length
    }
}
fn main() {
    let rect = Rectangle{
        width : 30,
        length : 50,

    };
    println!("{}",rect.area());
    // 格式化输出
    println!("{:#?}",rect);
}

1.10、多个参数方法

#[derive(Debug)]
struct Rectangle{
    width : u32,
    length : u32,
}
// 定义方法
impl Rectangle {
    fn area(&self) -> u32{
        self.width * self.length
    }

    fn can_hold(&self,other:&Rectangle) -> bool{
        self.width > other.width && self.length > other.length
    }
}
fn main() {
    let rect = Rectangle{
        width : 30,
        length : 50,

    };
    let rect1 = Rectangle{
        width : 10,
        length : 40,

    };
    let rect2 = Rectangle{
        width : 35,
        length : 52,

    };
    println!("{}",rect.area());
    // 格式化输出
    println!("{:#?}",rect);

    // 调用多个方法的函数
    println!("{}",rect.can_hold(&rect1));
    println!("{}",rect.can_hold(&rect2));

}

1.11、关联函数

#[derive(Debug)]
struct Rectangle{
    width : u32,
    length : u32,
}
// 定义方法
impl Rectangle {
    // 定义关联函数
    fn square(size:u32)->Rectangle{
        Rectangle { 
            width: size, 
            length: size 
        }
    }
}
fn main() {
    // 调用关联函数 名:函数名
    let s = Rectangle::square(20); 

}

rust语言学习

推荐阅读: