7.14. Rust enumerated class

发布时间 : 2025-10-25 13:33:19 UTC      

Page Views: 9 views

Enumeration classes are not as simple as concepts in other programming languages in Rust, but they can still be used very easily:

7.14.1. Example #

#[derive(Debug)] enum Book { Papery, Electronic } fn main() { let book = Book::Papery; println!("{:?}", book); } 

Running result:

Papery 

Books are divided into paper books and electronic books.

If you are currently developing a library management system, you need to describe the different attributes of the two books (paper books have call numbers, e-books only URL), and you can add tuple attribute descriptions to enumerated class members:

enum Book { Papery(u32), Electronic(String), } let book = Book::Papery(1001); let ebook = Book::Electronic(String::from("url://...")); 

If you want to name the attribute, you can use the structure syntax:

enum Book { Papery { index: u32 }, Electronic { url: String }, } let book = Book::Papery{index: 1001}; 

Although you can name it this way, note that you cannot access the properties bound to the enumerated class as you would a structural body field. The method of access is in match in grammar.

Match syntax #

The purpose of enumeration is to classify a certain category of things, and the purpose of classification is to describe different situations. Based on this principle, enumerated classes often end up being processed by branch structures (switch in many languages). switch syntax is classic, but itis not supported in Rust, and many languages abandon it. switch reason is all because switch easy to exist due to forgetting to add break problem of serial operation is eliminated by security checks in languages such as Java and C#.

Rust passed match statement to implement the branch structure. Let’s first know how to use it. match handle enumerated classes:

7.14.2. Example #

fn main() { enum Book { Papery {index: u32}, Electronic {url: String}, } let book = Book::Papery{index: 1001}; let ebook = Book::Electronic{url: String::from("url...")}; match book { Book::Papery { index } => { println!("Papery book {}", index); }, Book::Electronic { url } => { println!("E-book {}", url); } } } 

Running result:

Papery book 1001 

match blocks can also be treated as function expressions, and it can also have a return value:

 Match Enumeration Class Instance{ Category 1=>Return value expression, Category 2=>Return value expression, ... } 

But all return value expressions must be of the same type!

If you define the additional properties of the enumerated class as tuples, in the match a name needs to be temporarily specified in the block:

7.14.3. Example #

enum Book { Papery(u32), Electronic {url: String}, } let book = Book::Papery(1001); match book { Book::Papery(i) => { println!("{}", i); }, Book::Electronic { url } => { println!("{}", url); } } 

In addition to branch selection for enumerated classes, match can also branch selection for data of integer, floating-point, character, and string slice reference (& str) types. Among them, although it is legal for floating point type to be selected by branch, it is not recommended because precision problems may lead to branch errors.

When selecting branches for non-enumerated classes, you must pay attention to handling exceptions, even if there is nothing to do in exceptional cases.Use an underscore as an exception \_ indicates:

7.14.4. Example #

fn main() { let t = "abc"; match t { "abc" => println!("Yes"), \_ => {}, } } 

Option enumerated class #

Option is an enumerated class in the Rust standard library that is used to fill in Rust’s lack of support null the white space of the reference.

Many languages support null is very convenient, but it also creates great problems. null inventors admit that “a convenient idea has caused a cumulative loss of 1 billion dollars.”

null often after developers treat everything as no null give the program a fatal blow: after all, as long as there is such an error, the operation of the program will be completely terminated.

To solve this problem, many languages are not allowed by default null , But at the language level null the appearance of (often used before the type? Symbol modification).

Java supports it by default null , but it can be done through @NotNull annotation restrictions appear null , this is a way to deal with it.

Rust does not allow null values at the language level at all. null the existence of, but helpless null small number of problems can be solved efficiently, so Rust introduces Option enumerated classes:

enum Option<T> { Some(T), None, } 

If you want to define a class that can be null, you can do this:

let opt = Option::Some("Hello"); 

If you want to target opt to perform something, you must first determine whether it is Option::None :

7.14.5. Example #

fn main() { let opt = Option::Some("Hello"); match opt { Option::Some(something) => { println!("{}", something); }, Option::None => { println!("opt is nothing"); } } } 

Running result:

Hello 

If your variable is null at first, consider the compiler. How does it know what type of variable is when the value is not null?

So the initial value is empty. Option type must be clear:

7.14.6. Example #

fn main() { let opt: Option<&str> = Option::None; match opt { Option::Some(something) => { println!("{}", something); }, Option::None => { println!("opt is nothing"); } } } 

Running result:

opt is nothing 

This design makes null programming difficult, but it is exactly what is needed to build a stable and efficient system. Due to Option is introduced by default by the Rust compiler and can be omitted when in use Option:: write directly None or Some() .

Option is a special enumeration class that can contain value branch selections:

7.14.7. Example #

fn main() { let t = Some(64); match t { Some(64) => println!("Yes"), \_ => println!("No"), } } 

if let Grammar #

7.14.8. Example #

let i = 0; match i { 0 => println!("zero"), \_ => {}, } 

Put the running result of the main function:

zero 

The purpose of this program is to determine whether I is the number 0, and if so, print it. zero .

Use it now if let syntax shortens this code:

let i = 0; if let 0 = i { println!("zero"); } 

The `` if let``syntax format is as follows:

 If let matching value=source variable{ Statement block } 

You can add one later else block to handle exceptions.

if let grammar can be thought of as distinguishing only two cases. The match “grammatical sugar” of a statement (grammatical sugar refersto a convenient substitute with the same principle as a grammar).

It still applies to enumerated classes:

7.14.9. Example #

fn main() { enum Book { Papery(u32), Electronic(String) } let book = Book::Electronic(String::from("url")); if let Book::Papery(index) = book { println!("Papery {}", index); } else { println!("Not papery book"); } } 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。