当前位置:网站首页>Structs in trust
Structs in trust
2022-06-27 19:56:00 【User 3147702】
1. introduction
In the previous article , We have introduced in detail Rust Basic syntax :
In many languages , We have all been exposed to the composite data type structure for a long time , In an object-oriented language , The concept of class is very similar , stay rust In language , Structs are also a useful and powerful data type , that , stay rust In language , How to define and use the data type structure ?
2. Definition of structure
stay rust in , Tuples 、 Array 、 Structures are composite structures of data , The difference between them is :
- Array : Each element must have the same data type ;
- Tuples : Each element has its own type ;
- Structure : Each element has its own type , And each element needs to be named .
The following is the definition of a typical structure :
struct User {
username: String,
email: String,
active: bool
}It seems that in addition to the field naming and type, it depends on rust Unique language format , Other aspects are not very different from the definition of structure in other languages .
rust It is also allowed to declare structures that do not name any fields , Such a structure is called “ Tuple structure ”, Equivalent to a named tuple :
struct Color(i32, i32, i32);- It should be noted that , Defined here uses String type , instead of &str type , because rust Unique life cycle issues , Will cause the use of &str Types can cause some trouble , The following chapters will cover .
3. The use of structures
3.1 Example
let user1 = User {
email: String::from("[email protected]"),
username: String:from("Amy"),
active: true
};
println!("{}'s email is {}", user1.username, user1.email);This is the use of a typical structure .
3.2 variability
If we want to modify a field of a structure variable , You must declare the structure variable as mutable mut:
let mut user1 = User {
email: String::from("[email protected]"),
username: String:from("Amy"),
active: false
};
user1.active = true;- It should be noted that ,rust It is not supported to declare some fields individually , Once the instance is declared mutable , It means that any field in the whole structure is variable .
3.3 The use of sugar to simplify the structure
1. Simplification of variable name and field name
fn build_user(email: String, username: String) -> User {
User {
email,
username,
active: true
}
}2. Fields from other instances
let user2 = User {
email: String::from("[email protected]"),
username: String::from("Tom"),
..user1
} Here grammar ..user1 Indicates that the remaining fields are from user1 example .
4. Method
In an object-oriented language , Objects have their own properties , Often there are many behaviors , These behaviors are generally defined as classes method, That's the method .
Even in C In a non object-oriented language like C , You can also add behavior to the structure through function pointers , stay rust in , It also supports adding methods to structures .
4.1 Define methods
struct Rectangle {
width: u32,
height: u32,
}
impl Rectangle {
fn area(&self) -> u32 {
self.width * self.height
}
}
fn main() {
let rect1 = Rectangle {
width: 30,
height: 50,
};
println!(
"The area of the rectangle is {} square pixels.",
rect1.area()
);
} In the above code ,struct Keyword defines a structure Rectangle, And then through impl Bind several methods to the structure , By way of example . The operator , We can implement the method call .
stay impl In block , We can use Self As an alias of the original type .
For a structure ,impl Blocks do not have only one , A structure can have many impl block , You can even create a separate for each method or correlation function impl block .
4.2 correlation function
And in the impl Defined in does not need to be passed &self The function of the parameter is called ” correlation function “, Very similar to java In language static Method , for example :
impl Rectangle {
fn square(size: u32) -> Rectangle {
Rectangle {
width: size,
height: size,
}
}
}
fn main() {
let rectangle = Rectangle::square(4);
} As in the example above , The association function of the structure needs to be connected with the structure type name through :: Symbol to call .
边栏推荐
猜你喜欢

拥抱云原生:江苏移动订单中心实践

GIS remote sensing R language learning see here

从指令交读掌握函数调用堆栈详细过程

“我让这个世界更酷”2022华清远见研发产品发布会圆满成功

今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献

连接集成开发专题月 | 企业主数据治理的驱动因素

什么是SSR/SSG/ISR?如何在AWS上托管它们?

The Fifth Discipline: the art and practice of learning organization

散列表(Hash)-复习

SQL Server - window function - solve the problem of filtering consecutive n records
随机推荐
GIS遥感R语言学习看这里
Workflow automation low code is the key
拥抱云原生:江苏移动订单中心实践
运算符的基础知识
[login interface]
Bit. Store: long bear market, stable stacking products may become the main theme
【bug】联想小新出现问题,你的PIN不可用。
429-二叉树(108. 将有序数组转换为二叉搜索树、538. 把二叉搜索树转换为累加树、 106.从中序与后序遍历序列构造二叉树、235. 二叉搜索树的最近公共祖先)
NVIDIA Clara-AGX-Developer-Kit installation
经纬度分析
redis集群系列三
买股票在券商经理的开户链接上开户安全吗?求大神赐教
Erreur Keil de Huada Single Chip Computer La solution de Weak
Embracing cloud Nativity: Practice of Jiangsu Mobile order center
通过 Cargo 管理 Rust 项目
UE4:Build Configuration和Config的解释
UE4实现长按功能
308. 二维区域和检索 - 可变 线段树/哈希
嵌入式软件开发中必备软件工具
Crawl national laws and Regulations Database