Fixed mistakes and missing semicolon in enum
This commit is contained in:
parent
ef190d9e76
commit
c241eebbb6
|
@ -3,7 +3,7 @@
|
|||
|
||||
```rust,editable
|
||||
|
||||
// fix the errors
|
||||
// Fix the errors
|
||||
enum Number {
|
||||
Zero,
|
||||
One,
|
||||
|
@ -25,18 +25,18 @@ enum Number2 {
|
|||
|
||||
|
||||
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!(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
|
||||
|
||||
// fill in the blank
|
||||
// Fill in the blank
|
||||
enum Message {
|
||||
Quit,
|
||||
Move { x: i32, y: i32 },
|
||||
|
@ -45,17 +45,17 @@ enum Message {
|
|||
}
|
||||
|
||||
fn main() {
|
||||
let msg1 = Message::Move{__}; // instantiating with x = 1, y = 2
|
||||
let msg2 = Message::Write(__); // instantiating with "hello, world!"
|
||||
let msg1 = Message::Move{__}; // Instantiating with x = 1, y = 2
|
||||
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
|
||||
|
||||
// fill in the blank and fix the error
|
||||
// Fill in the blank and fix the error
|
||||
enum Message {
|
||||
Quit,
|
||||
Move { x: i32, y: i32 },
|
||||
|
@ -72,7 +72,7 @@ fn main() {
|
|||
panic!("NEVER LET THIS RUNοΌ");
|
||||
}
|
||||
|
||||
println!("Success!")
|
||||
println!("Success!");
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -80,7 +80,7 @@ fn main() {
|
|||
|
||||
```rust,editable
|
||||
|
||||
// fill in the blank and fix the errors
|
||||
// Fill in the blank and fix the errors
|
||||
enum Message {
|
||||
Quit,
|
||||
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
|
||||
|
||||
// fill in the blank to make the `println` work.
|
||||
// also add some code to prevent the `panic` from running.
|
||||
// Fill in the blank to make the `println` work.
|
||||
// Also add some code to prevent the `panic` from running.
|
||||
fn main() {
|
||||
let five = Some(5);
|
||||
let six = plus_one(five);
|
||||
|
@ -118,7 +118,7 @@ fn main() {
|
|||
if let __ = six {
|
||||
println!("{}", n);
|
||||
|
||||
println!("Success!")
|
||||
println!("Success!");
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
|
@ -166,12 +166,12 @@ impl List {
|
|||
// depends on the variant of `self`
|
||||
// `self` has type `&List`, and `*self` has type `List`, matching on a
|
||||
// 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.
|
||||
// See https://doc.rust-lang.org/edition-guide/rust-2018/ownership-and-lifetimes/default-match-bindings.html
|
||||
match *self {
|
||||
// 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(),
|
||||
// Base Case: An empty list has zero length
|
||||
Nil => 0
|
||||
|
|
Loadingβ¦
Reference in New Issue