rust -结构体学习

通过对《Rust 程序设计语言》,《通过例子学 Rust 中文版》以及令狐一冲老师对相关知识点的学习总结而成。

rust -结构体学习

  • 1,定义结构体
  • 2 创建结构体实例
  • 3 修改结构体的字段
  • 4 参数名字和字段名字相同的简写方法
  • 5 从其他结构体创建实例
  • 6 元组结构体
  • 7 没有任何字段的类单元结构体
  • 8 打印结构体

1,定义结构体

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

2 创建结构体实例

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

3 修改结构体的字段

fn main() {

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

	//2 创建结构体实例
    let james = User {
        email: String::from("[email protected]"),
        username: String::from("james"),
        active: true,
        sign_in_count: 100,
    };

	//3 修改结构体的字段
    let mut tom = User {
        email: String::from("[email protected]"),
        username: String::from("tom"),
        active: true,
        sign_in_count: 1000,
    };

    tom.sign_in_count = 100;

    println!("Hello, world!");
}

4 参数名字和字段名字相同的简写方法

fn main() {

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

	//2 创建结构体实例
    let james = User {
        email: String::from("[email protected]"),
        username: String::from("james"),
        active: true,
        sign_in_count: 100,
    };

	//3 修改结构体的字段
    let mut tom = User {
        email: String::from("[email protected]"),
        username: String::from("tom"),
        active: true,
        sign_in_count: 1000,
    };

    tom.sign_in_count = 100;

	//4 参数名字和字段名字相同的简写方法
    let active = true;
    let email = String::from("[email protected]");
    let username = String::from("peter");
    let sign_in_count = 200;

    let peter = User {
        active,
        username,
        email,
        sign_in_count,
    };

    println!("Hello, world!");
}

5 从其他结构体创建实例

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

fn main() {
	//1,定义结构体
    struct User {
        active: bool,
        username: String,
        email: String,
        sign_in_count: u64,
    };

	//2 创建结构体实例
    let james = User {
        email: String::from("[email protected]"),
        username: String::from("james"),
        active: true,
        sign_in_count: 100,
    };

	//3 修改结构体的字段
    let mut tom = User {
        email: String::from("[email protected]"),
        username: String::from("tom"),
        active: true,
        sign_in_count: 1000,
    };

    tom.sign_in_count = 100;

	//4 参数名字和字段名字相同的简写方法
    let active = true;
    let email = String::from("[email protected]");
    let username = String::from("peter");
    let sign_in_count = 200;

    let peter = User {
        active,
        username,
        email,
        sign_in_count,
    };

	//5 从其他结构体创建实例
    let jimmy = User {
        username: String::from("jimmy"),
        ..peter
    };

    println!("jimmy is {:#?}", jimmy);
    
    println!("Hello, world!");
}

6 元组结构体

元组结构体的特征:

  • 字段没有名字
  • 用圆括号
#[derive(Debug)]
struct User {
    active: bool,
    username: String,
    email: String,
    sign_in_count: u64,
};

fn main() {
	//1,定义结构体
    struct User {
        active: bool,
        username: String,
        email: String,
        sign_in_count: u64,
    };

	//2 创建结构体实例
    let james = User {
        email: String::from("[email protected]"),
        username: String::from("james"),
        active: true,
        sign_in_count: 100,
    };

	//3 修改结构体的字段
    let mut tom = User {
        email: String::from("[email protected]"),
        username: String::from("tom"),
        active: true,
        sign_in_count: 1000,
    };

    tom.sign_in_count = 100;

	//4 参数名字和字段名字相同的简写方法
    let active = true;
    let email = String::from("[email protected]");
    let username = String::from("peter");
    let sign_in_count = 200;

    let peter = User {
        active,
        username,
        email,
        sign_in_count,
    };

	//5 从其他结构体创建实例
    let jimmy = User {
        username: String::from("jimmy"),
        ..peter
    };

    println!("jimmy is {:#?}", jimmy);
    
    //6 元组结构体
    struct Color(i32, i32, i32);
    struct Point(i32, i32, i32);
    let black = Color(0, 0, 0);
    let origin = Point(0, 0, 0);

	println!("black.r = {}, black.g = {}, black.b = {}", black.0, black.1, black.2);

    println!("Hello, world!");
}

7 没有任何字段的类单元结构体

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

fn main() {
	//1,定义结构体
    struct User {
        active: bool,
        username: String,
        email: String,
        sign_in_count: u64,
    };

	//2 创建结构体实例
    let james = User {
        email: String::from("[email protected]"),
        username: String::from("james"),
        active: true,
        sign_in_count: 100,
    };

	//3 修改结构体的字段
    let mut tom = User {
        email: String::from("[email protected]"),
        username: String::from("tom"),
        active: true,
        sign_in_count: 1000,
    };

    tom.sign_in_count = 100;

	//4 参数名字和字段名字相同的简写方法
    let active = true;
    let email = String::from("[email protected]");
    let username = String::from("peter");
    let sign_in_count = 200;

    let peter = User {
        active,
        username,
        email,
        sign_in_count,
    };

	//5 从其他结构体创建实例
    let jimmy = User {
        username: String::from("jimmy"),
        ..peter
    };

    println!("jimmy is {:#?}", jimmy);
    
    //6 元组结构体
    struct Color(i32, i32, i32);
    struct Point(i32, i32, i32);
    let black = Color(0, 0, 0);
    let origin = Point(0, 0, 0);

	println!("black.r = {}, black.g = {}, black.b = {}", black.0, black.1, black.2);

	//7 没有任何字段的类单元结构体
	struct Student {};
	
    println!("Hello, world!");
}

8 打印结构体

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

fn main() {
	//1,定义结构体
    struct User {
        active: bool,
        username: String,
        email: String,
        sign_in_count: u64,
    };

	//2 创建结构体实例
    let james = User {
        email: String::from("[email protected]"),
        username: String::from("james"),
        active: true,
        sign_in_count: 100,
    };

	//3 修改结构体的字段
    let mut tom = User {
        email: String::from("[email protected]"),
        username: String::from("tom"),
        active: true,
        sign_in_count: 1000,
    };

    tom.sign_in_count = 100;

	//4 参数名字和字段名字相同的简写方法
    let active = true;
    let email = String::from("[email protected]");
    let username = String::from("peter");
    let sign_in_count = 200;

    let peter = User {
        active,
        username,
        email,
        sign_in_count,
    };

	//5 从其他结构体创建实例
    let jimmy = User {
        username: String::from("jimmy"),
        ..peter
    };

    println!("jimmy is {:#?}", jimmy);
    
    //6 元组结构体
    struct Color(i32, i32, i32);
    struct Point(i32, i32, i32);
    let black = Color(0, 0, 0);
    let origin = Point(0, 0, 0);

	println!("black.r = {}, black.g = {}, black.b = {}", black.0, black.1, black.2);

	//7 没有任何字段的类单元结构体
	struct Student {};
	
	//8 打印结构体
	//比如 打印jimmy的信息
	println!("jimmy is {:?}", jimmy);	//所有打印显示在一行
	println!("jimmy is {:#?}", jimmy);	//打印按照结构体的方式去输出
	
    println!("Hello, world!");
}

你可能感兴趣的:(编程语言-rust,rust,rust,-结构体学习)