- Rust Quick Start Guide
- Daniel Arbuckle
- 297字
- 2021-06-10 19:46:06
Data structures
Creating a data structure is one of the ways to add a new data type to Rust. A data structure is a group of variables that have been attached to each other, resulting in a single new data type that means all of these, together.
A new structure is defined using the struct keyword:
pub struct Constrained {
pub min: i32,
pub max: i32,
current: i32,
}
Here, we've defined a structure called Constrained, which is made up of three different 32-bit unsigned integer variables. The structure itself is public, meaning that it can be used outside of the module where it's defined.
The min and max contained variables are also public, but that means something slightly different. It means that anywhere we have a Constrained value, we can access the min and max contained values directly. The current value, on the other hand, is private, which means that it can be directly accessed only within the module where the structure is defined. We can define functions in that module with the express purpose of accessing the data contained in private structure members, but the members themselves are not part of the structure's public interface, even if the structure itself is public.
To access min and max, we can use the same . symbol that we've seen previously in a few places. So, if cons is a mutable Constrained value, then we can do things like this:
cons.min = 5;
- 從零構建知識圖譜:技術、方法與案例
- CentOS 7 Linux Server Cookbook(Second Edition)
- VMware vSphere 6.7虛擬化架構實戰指南
- Hadoop+Spark大數據分析實戰
- YARN Essentials
- Learning Neo4j 3.x(Second Edition)
- MySQL數據庫管理與開發實踐教程 (清華電腦學堂)
- Python數據分析從0到1
- 21天學通C++(第5版)
- Application Development with Parse using iOS SDK
- Web前端開發技術:HTML、CSS、JavaScript
- Android編程權威指南(第4版)
- Test-Driven iOS Development with Swift
- Cinder:Begin Creative Coding
- 劍指大數據:企業級電商數據倉庫項目實戰(精華版)