Rust语言

关注公众号 jb51net

关闭
首页 > 软件编程 > Rust语言 > rust struct

解析rust中的struct

作者:fan_music

自定义的数据类型,为相关联的值命名,打包成有意义的组合,类似python的dict,但是赋值的时候可以不按顺序,本文给大家介绍下rust中的struct知识,感兴趣的朋友一起看看吧

定义struct

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

实例化struct

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

fn main(){
    let user1 = User{
        username: String::from("xxxx"),
        email: String::from("xxxx@163.com"),
        active: true,
        sign_in_count:556,
    };
    println!("username:{}", user1.username);
    println!("email:{}", user1.email);
    println!("active:{}", user1.active);
    println!("sign_in_count:{}", user1.sign_in_count);
}

取得struct里面的某个值

使用点标记法

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

fn main(){
    let mut user1 = User{
        username: String::from("xxxx"),
        email: String::from("xxxx@163.com"),
        active: true,
        sign_in_count:556,
    };
    // 注意 user1 必须是可变的 
    user1.username = String::from("hhhhhhh");
    println!("username:{}", user1.username);
    println!("email:{}", user1.email);
    println!("active:{}", user1.active);
    println!("sign_in_count:{}", user1.sign_in_count);
}

注意

一旦struct的实例是可变的,那么实例中的所有字段都是可以变的

struct作为函数的放回值

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

fn build_user(email: String, username: String)-> User{
    User{
        email: email,
        username: username,
        active: true,
        sign_in_count:1,
    }
}

fn main(){
    let email = String::from("xxxx@163.com");
    let username = String::from("llllll");
    let user1 = build_user(email, username);
    println!("username:{}", user1.username);
    println!("email:{}", user1.email);
    println!("active:{}", user1.active);
    println!("sign_in_count:{}", user1.sign_in_count);
}

字段初始化简写

当字段名与字段值对应变量名相同时,就可以使用字段初始化简写的方式

fn build_user(email: String, username: String)-> User{
    User{
        email,
        username,
        active: true,
        sign_in_count:1,
    }
}

struct更新语法

当你想基于某个struct实例来创建一个新实例的时候,可以使用struct更新语法

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

fn build_user(email: String, username: String)-> User{
    User{
        email,
        username,
        active: true,
        sign_in_count:1,
    }
}

fn main(){
    let email = String::from("xxxx@163.com");
    let username = String::from("llllll");
    let user1 = build_user(email, username);
    // user2 email 重新赋值
    // user2 其他变量 使用 user1 的值
    // String 类型会被引用 从而失效
    let user2 = User{
        email: String::from("user2@163.com"),
        ..user1
    };
    // user1.username 被 user2.username 引用 从而失效
    // println!("username:{}", user1.username);
    println!("username:{}", user2.username);
    println!("email:{}", user1.email);
    println!("email:{}", user2.email);
    println!("active:{}", user1.active);
    println!("sign_in_count:{}", user1.sign_in_count);
}

tuple struct

struct Color(i32, i32, i32);
struct Point(i32, i32, i32);
let black = Color(0, 2, 3);
let origin = Point(3, 2, 3);

Unit-Like Struct(没有任何字段)

struct数据的所有权

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

什么事struct

#[derive(Debug)]
struct Rectangle{
    width: u32,
    length: u32,
}
fn main(){
    let rect = Rectangle{
        width: 22,
        length: 44,
    };
    println!("{}", area(&rect));
    println!("{:?}", rect);
    println!("{:#?}", rect);
}
fn area(rect: &Rectangle)-> u32{
    rect.length * rect.width
}

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: 33,
        length: 44,
    };
    println!("{}", rect.area());
    println!("{:#?}", rect);
}

​​​​​​​​​​​​​​方法调用的运算符

p1.distance(&p2);
(&p1).distance(&p2);

关联函数

String::from();
#[derive(Debug)]
struct Rectangle{
    width: u32,
    length: u32,
}
impl Rectangle{
    fn square(width: u32, length: u32)->Rectangle{
        Rectangle{
            width,
            length,
        }
    }
}
fn main(){
    let rect = Rectangle::square(33, 11);
    println!("width:{}", rect.width);
    println!("length:{}", rect.length);
    println!("{:#?}", rect);
}

多个impl块

到此这篇关于解析rust中的struct的文章就介绍到这了,更多相关rust struct内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文