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,
        };
    }
}
相关推荐
‍。。。9 小时前
使用Rust实现http/https正向代理
http·https·rust
Source.Liu9 小时前
【用Rust写CAD】第二章 第四节 函数
开发语言·rust
monkey_meng9 小时前
【Rust中的迭代器】
开发语言·后端·rust
余衫马9 小时前
Rust-Trait 特征编程
开发语言·后端·rust
monkey_meng9 小时前
【Rust中多线程同步机制】
开发语言·redis·后端·rust
hikktn17 小时前
如何在 Rust 中实现内存安全:与 C/C++ 的对比分析
c语言·安全·rust
睡觉谁叫~~~17 小时前
一文解秘Rust如何与Java互操作
java·开发语言·后端·rust
音徽编程17 小时前
Rust异步运行时框架tokio保姆级教程
开发语言·网络·rust
梦想画家1 天前
快速解锁Rust Slice特性
开发语言·rust·slice
良技漫谈1 天前
Rust移动开发:Rust在iOS端集成使用介绍
后端·程序人生·ios·rust·objective-c·swift