Enumeration classes are not as simple as concepts in other programming languages in Rust, but they can still be used very easily: Running result: 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: If you want to name the attribute, you can use the structure syntax: 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 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). Rust passed Running result: 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 In addition to branch selection for enumerated classes, 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 Many languages support To solve this problem, many languages are not allowed by default Java supports it by default Rust does not allow null values at the language level at all. If you want to define a class that can be null, you can do this: If you want to target Running result: 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. Running result: This design makes null programming difficult, but it is exactly what is needed to build a stable and efficient system. Due to Option is a special enumeration class that can contain value branch selections: Put the running result of the main function: The purpose of this program is to determine whether I is the number 0, and if so, print it. Use it now The `` if let``syntax format is as follows: You can add one later It still applies to enumerated classes: 7.14.1. Example #
#[derive(Debug)]
enum Book {
Papery, Electronic
}
fn main() {
let book = Book::Papery;
println!("{:?}", book);
}
Papery
enum Book {
Papery(u32),
Electronic(String),
}
let book = Book::Papery(1001);
let ebook = Book::Electronic(String::from("url://..."));
enum Book {
Papery { index: u32 },
Electronic { url: String },
}
let book = Book::Papery{index: 1001};
match
in grammar.Match syntax #
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#.
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);
}
}
}
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,
...
}
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);
}
}
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.
\_
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.
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.
null
, But at the language level
null
the appearance of (often used before the type? Symbol modification).
null
, but it can be done through
@NotNull
annotation restrictions appear
null
, this is a way to deal with it.
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,
}
let opt = Option::Some("Hello");
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");
}
}
}
Hello
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");
}
}
}
opt is nothing
Option
is introduced by default by the Rust compiler and can be omitted when in use
Option::
write directly
None
or
Some()
. 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"),
\_ => {},
}
zero
zero
.
if
let
syntax shortens this code:let i = 0;
if let 0 = i {
println!("zero");
}
If let matching value=source variable{
Statement block
}
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). 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");
}
}