网站项目管理系统,网站标题间隔符,企业logo设计网站,wordpress邮件找客户端0 前言
结构体#xff0c;struct#xff0c;听到这个名字大家可能会感觉“哇#xff01;好熟悉#xff01;”#xff0c;毕竟在大学时学习c/cpp的时候已经接触过很多次了#xff0c;实际上也确实很类似#xff0c;即使有些不同#xff0c;比如“所有权系统”#xff…0 前言
结构体struct听到这个名字大家可能会感觉“哇好熟悉”毕竟在大学时学习c/cpp的时候已经接触过很多次了实际上也确实很类似即使有些不同比如“所有权系统”上一节提到了、“生命周期”还没提到、“模式匹配”也很熟悉吧、Trait还没提到也大多数是在更后面才会提到。 总体来讲Rust的struct在语法上与c/cpp类似但因入了一些新的概念和特性提供更高级、更安全的编程体验不过这一节只是认识struct所以学起来会比较轻松。
原文链接Rust程序设计语言 1 定义与实例化
让我们来定义和实例化一个结构体
// 1 定义
struct User {active: bool,username: String,email: String,sign_in_count: u64,
}fn main() {// 2 实例化let user0 User {email: String::from(someoneexample.com),username: String::from(someusername123),active: true,sign_in_count: 1,}; // 整个不可变// 3 实例化可变let mut user1 User {email: String::from(someoneexample.com),username: String::from(someusername123),active: true,sign_in_count: 1,}; // 整个可变不能部分可变// 4 可变user1.email String::from(anotheremailexample.com);// 5 简便实例化// 使用结构体更新语法从其他实例创建实例let user2 User {email: String::from(anotherexample.com),..user1 // 必须放在最后};
}// 6 函数创建
fn build_user(email: String, username: String) - User {User {// 7 简洁创建email, // 字段初始化的简洁写法username,active: true,sign_in_count: 1,}
}除了以上方式还可以没有明明字段的方式来创建比如
struct Color(i32, i32, i32);
struct Point(i32, i32, i32);fn main() {let black Color(0, 0, 0);let origin Point(0, 0, 0);
}但是要注意虽然Color与Point的内容看起来一样但他们是不同的类型如果你一个函数入参设定为Color那么不能用Point
不过有一个比较复杂的内容是“所有权”“可以使结构体存储被其他对象拥有的数据的引用不过这么做的话需要用上生命周期lifetime”这一点内容会在后面笔记中再展开。 2 示例程序
在这一部分可以注意 1、结构体定义 2、创建实例 3、impl关联函数 4、多个关联函数 5、非方法 6、dbg调试 7、函数调用含入参
#[derive(Debug)] // 调试
// 结构体
struct Rectangle {width: u32,height: u32,
}// 关联函数
impl Rectangle {// self使用注意selffn area(self) - u32 {self.width * self.height}// 更多的函数fn can_hold(self, other: Rectangle) - bool {self.width other.width self.height other.height}
}
// 多个关联函数 is ok
impl Rectangle {// 不以self为第一参数的关联函数不是方法// 因为他们并不作用于一个结构体的实例而是在类型上定义的就像String::from// 需要以结构体名和::语法来调用见下文fn square(size: u32) - Rectangle {Rectangle {width: size,height: sieze,}}
}fn main() {let rect1 Rectangle {width: 30,height: 50,};dbg!(rect1); // 如果你对调试有兴趣请阅读原文let rect2 Rectangle {width: 10,height: 40,};let rect3 Rectangle {width: 60,height: 45,};// 方法调用println!(The area of the rectangle is {} square pixels.,rect1.area());// 方法调用、参数传入println!(Can rect1 hold rect2 ? {}, rect1.can_hold(rect2));// 不是方法以此种方式调用let sq Rectangle::square(3);
}