mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-24 13:09:40 +00:00
Fixed spellings clarified comments in trates
This commit is contained in:
@ -81,8 +81,8 @@ fn main() {
|
|||||||
1. 🌟🌟
|
1. 🌟🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// fill in the two impl blocks to make the code work
|
// Fill in the two impl blocks to make the code work.
|
||||||
// DON'T modify the code in `main`
|
// DON'T modify the code in `main`.
|
||||||
trait Hello {
|
trait Hello {
|
||||||
fn say_hi(&self) -> String {
|
fn say_hi(&self) -> String {
|
||||||
String::from("hi")
|
String::from("hi")
|
||||||
@ -107,7 +107,7 @@ fn main() {
|
|||||||
assert_eq!(t.say_hi(), "Hi, I'm your new teacher");
|
assert_eq!(t.say_hi(), "Hi, I'm your new teacher");
|
||||||
assert_eq!(t.say_something(), "I'm not a bad teacher");
|
assert_eq!(t.say_something(), "I'm not a bad teacher");
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -135,7 +135,7 @@ impl Inches {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ADD some attributes to make the code work!
|
// ADD some attributes to make the code work!
|
||||||
// DON'T modify other codes!
|
// DON'T modify other code!
|
||||||
struct Seconds(i32);
|
struct Seconds(i32);
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
@ -171,23 +171,23 @@ In Rust, many of the operators can be overloaded via traits. That is, some opera
|
|||||||
|
|
||||||
use std::ops;
|
use std::ops;
|
||||||
|
|
||||||
// implement fn multiply to make the code work
|
// Implement fn multiply to make the code work.
|
||||||
// As mentiond above, `+` needs `T` to implement `std::ops::Add` Trait
|
// As mentiond above, `+` needs `T` to implement `std::ops::Add` Trait.
|
||||||
// so, what about `*` ? You can find the answer here: https://doc.rust-lang.org/core/ops/
|
// So, what about `*`? You can find the answer here: https://doc.rust-lang.org/core/ops/
|
||||||
fn multipl
|
fn multipl
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
assert_eq!(6, multiply(2u8, 3u8));
|
assert_eq!(6, multiply(2u8, 3u8));
|
||||||
assert_eq!(5.0, multiply(1.0, 5.0));
|
assert_eq!(5.0, multiply(1.0, 5.0));
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
4. 🌟🌟🌟
|
4. 🌟🌟🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// fix the errors, DON'T modify the code in `main`
|
// Fix the errors, DON'T modify the code in `main`.
|
||||||
use std::ops;
|
use std::ops;
|
||||||
|
|
||||||
struct Foo;
|
struct Foo;
|
||||||
@ -217,12 +217,12 @@ impl ops::Sub<Foo> for Bar {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// DON'T modify the below code
|
// DON'T modify the code below.
|
||||||
// you need to derive some trait for FooBar to make it comparable
|
// You need to derive some trait for FooBar to make it comparable.
|
||||||
assert_eq!(Foo + Bar, FooBar);
|
assert_eq!(Foo + Bar, FooBar);
|
||||||
assert_eq!(Foo - Bar, BarFoo);
|
assert_eq!(Foo - Bar, BarFoo);
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -232,8 +232,8 @@ Instead of a concrete type for the item parameter, we specify the impl keyword a
|
|||||||
5. 🌟🌟🌟
|
5. 🌟🌟🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// implement `fn summary` to make the code work
|
// Implement `fn summary` to make the code work.
|
||||||
// fix the errors without removing any code line
|
// Fix the errors without removing any code line
|
||||||
trait Summary {
|
trait Summary {
|
||||||
fn summarize(&self) -> String;
|
fn summarize(&self) -> String;
|
||||||
}
|
}
|
||||||
@ -281,14 +281,14 @@ fn main() {
|
|||||||
println!("{:?}", weibo);
|
println!("{:?}", weibo);
|
||||||
}
|
}
|
||||||
|
|
||||||
// implement `fn summary` below
|
// Implement `fn summary` below.
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Returning Types that Implement Traits
|
### Returning Types that Implement Traits
|
||||||
We can also use the impl Trait syntax in the return position to return a value of some type that implements a trait.
|
We can also use the impl Trait syntax in the return position to return a value of some type that implements a trait.
|
||||||
|
|
||||||
However, you can only use impl Trait if you’re returning a single type, using Trait Objects instead when you really need to return serveral types.
|
However, you can only use impl Trait if you’re returning a single type, use Trait Objects instead when you really need to return several types.
|
||||||
|
|
||||||
6. 🌟🌟
|
6. 🌟🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
@ -313,7 +313,7 @@ impl Animal for Cow {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Returns some struct that implements Animal, but we don't know which one at compile time.
|
// Returns some struct that implements Animal, but we don't know which one at compile time.
|
||||||
// FIX the erros here, you can make a fake random, or you can use trait object
|
// FIX the errors here, you can make a fake random, or you can use trait object.
|
||||||
fn random_animal(random_number: f64) -> impl Animal {
|
fn random_animal(random_number: f64) -> impl Animal {
|
||||||
if random_number < 0.5 {
|
if random_number < 0.5 {
|
||||||
Sheep {}
|
Sheep {}
|
||||||
@ -340,7 +340,7 @@ fn main() {
|
|||||||
assert_eq!(sum(1, 2), 3);
|
assert_eq!(sum(1, 2), 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
// implement `fn sum` with trait bound in two ways
|
// Implement `fn sum` with trait bound in two ways.
|
||||||
fn sum<T>(x: T, y: T) -> T {
|
fn sum<T>(x: T, y: T) -> T {
|
||||||
x + y
|
x + y
|
||||||
}
|
}
|
||||||
@ -348,7 +348,7 @@ fn sum<T>(x: T, y: T) -> T {
|
|||||||
8. 🌟🌟
|
8. 🌟🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// FIX the errors
|
// FIX the errors.
|
||||||
struct Pair<T> {
|
struct Pair<T> {
|
||||||
x: T,
|
x: T,
|
||||||
y: T,
|
y: T,
|
||||||
@ -388,10 +388,10 @@ fn main() {
|
|||||||
9. 🌟🌟🌟
|
9. 🌟🌟🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// fill in the blanks to make it work
|
// Fill in the blanks to make it work
|
||||||
fn example1() {
|
fn example1() {
|
||||||
// `T: Trait` is the commonly used way
|
// `T: Trait` is the commonly used way.
|
||||||
// `T: Fn(u32) -> u32` specifies that we can only pass a closure to `T`
|
// `T: Fn(u32) -> u32` specifies that we can only pass a closure to `T`.
|
||||||
struct Cacher<T: Fn(u32) -> u32> {
|
struct Cacher<T: Fn(u32) -> u32> {
|
||||||
calculation: T,
|
calculation: T,
|
||||||
value: Option<u32>,
|
value: Option<u32>,
|
||||||
@ -424,7 +424,7 @@ fn example1() {
|
|||||||
|
|
||||||
|
|
||||||
fn example2() {
|
fn example2() {
|
||||||
// We can also use `where` to constrain `T`
|
// We can also use `where` to construct `T`
|
||||||
struct Cacher<T>
|
struct Cacher<T>
|
||||||
where T: Fn(u32) -> u32,
|
where T: Fn(u32) -> u32,
|
||||||
{
|
{
|
||||||
@ -465,7 +465,7 @@ fn main() {
|
|||||||
example1();
|
example1();
|
||||||
example2();
|
example2();
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user