Rust- 结构体

In Rust, a struct (short for "structure") is a custom data type that lets you name and package together multiple related values that make up a meaningful group. If you're coming from an object-oriented language, a struct is like an object's data attributes.

Here's an example of a struct in Rust:

rust 复制代码
struct User {
    username: String,
    email: String,
    sign_in_count: u64,
    active: bool,
}

// You can create an instance of the struct like this:
let user1 = User {
    email: String::from("someone@163.com"),
    username: String::from("someusername"),
    active: true,
    sign_in_count: 1,
};

This code defines a User struct that has four fields: username, email, sign_in_count, and active. The struct is then instantiated with the User { ... } syntax.

You can access the fields in a struct instance with dot notation:

rust 复制代码
println!("User's email: {}", user1.email);

Structs are also capable of having methods, which are defined within an impl block:

rust 复制代码
struct Rectangle {
    width: u32,
    height: u32,
}

impl Rectangle {
    fn area(&self) -> u32 {
        self.width * self.height
    }
}

let rect1 = Rectangle { width: 30, height: 50 };

println!(
    "The area of the rectangle is {} square pixels.",
    rect1.area()
);

In the above code, Rectangle is defined with width and height fields, and an area method is defined for it. The area method can be called on instances of Rectangle with dot notation, just like fields.

There are a few different types of structs in Rust, each with different use cases:

  • Classic structs, which are named and have a set of named fields. This is the most commonly used.

  • Tuple structs, which are named and have a set of unnamed fields. These are useful when you want to give a name to a tuple.

  • Unit structs, which are field-less, are useful for cases when you need to implement a trait on some type but don't have any data you need to store in the type itself.

rust 复制代码
// Tuple struct
struct Color(u8, u8, u8);

let white = Color(255, 255, 255);

// Unit struct
struct Unit;

A comprehensive case is as follows:

rust 复制代码
fn main() {
    /*
       元组结构体
       struct Pair(String, i32);

       C语言风格结构体
       struct 结构体名称 {
           ...
       }

       单元结构体,不带字段,在泛型中很有用。
       type Unit


       struct 结构体名称 {
           字段1:数据类型
           字段2:数据类型
           ...
       }

       let 实例名称 = 结构体名称 {
           字段1:数据1
           字段2:数据2
           ...
       }
    */

    let s = Study {
        name: String::from("Rust"),
        target: String::from("可以熟练书写Rust程序"),
        spend: 36,
    };

    println!("{:?}", s);    // 输出:Study { name: "Rust", target: "可以熟练书写Rust程序", spend: 36 }
    println!("{}", s.name); // 输出:Rust

    let s3 = get_instance(
        String::from("Rust Programming Language"),
        String::from("熟练书写Rust程序"),
        36,
    );

    println!("{:?}", s3);   // 输出:Study { name: "Rust Programming Language", target: "熟练书写Rust程序", spend: 36 }
    // show(s3);

    /*
       impl 结构体 {
           fn 方法名(&self, 参数列表) 返回值 {
               // 方法体
           }
       }

       函数 可以直接调用,同一个程序不能出现2个相同的函数签名的函数,因为函数不归属任何owner.
       方法 归属某一个owner,不同的owner可以有相同的方法签名。

       &self 表示当前结构体的实例。也是结构体普通方法固定的第一个参数,其他参数可选。

       结构体静态方法
       fn 方法名(参数:数据类型,...) -> 返回类型 {
            // 方法体
       }
       调用方式  结构体名称::方法名(参数列表)
    */

    println!("spend {}", s3.get_spend());   // 输出:spend 36

    let s4 = Study::get_instance_another(
        String::from("Rust Programming Language"),
        String::from("熟练书写Rust程序"),
        36,
    );

    println!("s4 {:?}", s4);    // 输出:s4 Study { name: "Rust Programming Language", target: "熟练书写Rust程序", spend: 36 }

    /*
       单元结构体
       是一个类型,有且只有一个值()
    */

    // 元组结构体
    let pair = (String::from("Rust"), 1);
    println!("pair 包含{:?} and {:?}", pair.0, pair.1); // 输出:pair 包含"Rust" and 1

    // 解构元组结构体
    let (study, spend) = pair;
    println!("pair 包含{:?} and {:?}", study, spend);   // 输出:pair 包含"Rust" and 1
}

fn show(s: Study) {
    println!(
        "name is {} target is {} spend is {}",
        s.name, s.target, s.spend
    );
}

fn get_instance(name: String, target: String, spend: i32) -> Study {
    return Study {
        name,
        target,
        spend,
    };
}

#[derive(Debug)]
struct Study {
    name: String,
    target: String,
    spend: i32,
}

impl Study {
    fn get_spend(&self) -> i32 {
        return self.spend;
    }

    fn get_instance_another(name: String, target: String, spend: i32) -> Study {
        return Study {
            name,
            target,
            spend,
        };
    }
}
相关推荐
SomeB1oody6 小时前
【Rust自学】6.1. 定义枚举
开发语言·后端·rust
SomeB1oody7 小时前
【Rust自学】5.3. struct的方法(Method)
开发语言·后端·rust
itas10921 小时前
Rust调用C动态库
c语言·rust·bindgen·bindings·rust c绑定
SomeB1oody1 天前
【Rust自学】5.1. 定义并实例化struct
开发语言·后端·rust
m0_748236111 天前
Calcite Web 项目常见问题解决方案
开发语言·前端·rust
SomeB1oody2 天前
【Rust自学】4.1. 所有权:栈内存 vs. 堆内存
开发语言·后端·rust
SomeB1oody3 天前
【Rust自学】4.2. 所有权规则、内存与分配
开发语言·后端·rust
SomeB1oody3 天前
【Rust自学】4.5. 切片(Slice)
开发语言·后端·rust
编码浪子3 天前
构建一个rust生产应用读书笔记6-拒绝无效订阅者02
开发语言·后端·rust
baiyu333 天前
1小时放弃Rust(1): Hello-World
rust