mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-23 04:29:41 +00:00
Fixed mistakes and missing semicolon in enum
This commit is contained in:
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// fix the errors
|
// Fix the errors
|
||||||
enum Number {
|
enum Number {
|
||||||
Zero,
|
Zero,
|
||||||
One,
|
One,
|
||||||
@ -25,18 +25,18 @@ enum Number2 {
|
|||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// a enum variant can be converted to a integer by `as`
|
// An enum variant can be converted to a integer by `as`
|
||||||
assert_eq!(Number::One, Number1::One);
|
assert_eq!(Number::One, Number1::One);
|
||||||
assert_eq!(Number1::One, Number2::One);
|
assert_eq!(Number1::One, Number2::One);
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
2. 🌟 each enum variant can hold its own data.
|
2. 🌟 Each enum variant can hold its own data.
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// fill in the blank
|
// Fill in the blank
|
||||||
enum Message {
|
enum Message {
|
||||||
Quit,
|
Quit,
|
||||||
Move { x: i32, y: i32 },
|
Move { x: i32, y: i32 },
|
||||||
@ -45,17 +45,17 @@ enum Message {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let msg1 = Message::Move{__}; // instantiating with x = 1, y = 2
|
let msg1 = Message::Move{__}; // Instantiating with x = 1, y = 2
|
||||||
let msg2 = Message::Write(__); // instantiating with "hello, world!"
|
let msg2 = Message::Write(__); // Instantiating with "hello, world!"
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
3. 🌟🌟 we can get the data which a enum variant is holding by pattern match
|
3. 🌟🌟 We can get the data which an enum variant is holding by pattern match.
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// fill in the blank and fix the error
|
// Fill in the blank and fix the error
|
||||||
enum Message {
|
enum Message {
|
||||||
Quit,
|
Quit,
|
||||||
Move { x: i32, y: i32 },
|
Move { x: i32, y: i32 },
|
||||||
@ -72,7 +72,7 @@ fn main() {
|
|||||||
panic!("NEVER LET THIS RUN!");
|
panic!("NEVER LET THIS RUN!");
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -80,7 +80,7 @@ fn main() {
|
|||||||
|
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// fill in the blank and fix the errors
|
// Fill in the blank and fix the errors
|
||||||
enum Message {
|
enum Message {
|
||||||
Quit,
|
Quit,
|
||||||
Move { x: i32, y: i32 },
|
Move { x: i32, y: i32 },
|
||||||
@ -105,11 +105,11 @@ fn show_message(msg: Message) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
5. 🌟🌟 As there is no `null` in Rust, we have to use enum `Option<T>` to deal the cases when value is absent.
|
5. 🌟🌟 Since there is no `null` in Rust, we have to use enum `Option<T>` to deal with the cases when the value is absent.
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// fill in the blank to make the `println` work.
|
// Fill in the blank to make the `println` work.
|
||||||
// also add some code to prevent the `panic` from running.
|
// Also add some code to prevent the `panic` from running.
|
||||||
fn main() {
|
fn main() {
|
||||||
let five = Some(5);
|
let five = Some(5);
|
||||||
let six = plus_one(five);
|
let six = plus_one(five);
|
||||||
@ -118,7 +118,7 @@ fn main() {
|
|||||||
if let __ = six {
|
if let __ = six {
|
||||||
println!("{}", n);
|
println!("{}", n);
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
|
|
||||||
panic!("NEVER LET THIS RUN!");
|
panic!("NEVER LET THIS RUN!");
|
||||||
@ -133,7 +133,7 @@ fn plus_one(x: Option<i32>) -> Option<i32> {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
6. 🌟🌟🌟🌟 implement a `linked-list` via enums.
|
6. 🌟🌟🌟🌟 Implement a `linked-list` via enums.
|
||||||
|
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
@ -166,12 +166,12 @@ impl List {
|
|||||||
// depends on the variant of `self`
|
// depends on the variant of `self`
|
||||||
// `self` has type `&List`, and `*self` has type `List`, matching on a
|
// `self` has type `&List`, and `*self` has type `List`, matching on a
|
||||||
// concrete type `T` is preferred over a match on a reference `&T`
|
// concrete type `T` is preferred over a match on a reference `&T`
|
||||||
// after Rust 2018 you can use self here and tail (with no ref) below as well,
|
// After Rust 2018 you can use self here and tail (with no ref) below as well,
|
||||||
// rust will infer &s and ref tail.
|
// rust will infer &s and ref tail.
|
||||||
// See https://doc.rust-lang.org/edition-guide/rust-2018/ownership-and-lifetimes/default-match-bindings.html
|
// See https://doc.rust-lang.org/edition-guide/rust-2018/ownership-and-lifetimes/default-match-bindings.html
|
||||||
match *self {
|
match *self {
|
||||||
// Can't take ownership of the tail, because `self` is borrowed;
|
// Can't take ownership of the tail, because `self` is borrowed;
|
||||||
// instead take a reference to the tail
|
// Instead take a reference to the tail
|
||||||
Cons(_, ref tail) => 1 + tail.len(),
|
Cons(_, ref tail) => 1 + tail.len(),
|
||||||
// Base Case: An empty list has zero length
|
// Base Case: An empty list has zero length
|
||||||
Nil => 0
|
Nil => 0
|
||||||
|
Reference in New Issue
Block a user