commit
d49d36ba8e
|
@ -26,7 +26,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
2. π Hide the methods of the original type
|
2. π Hide the methods of the original type.
|
||||||
```rust,editable
|
```rust,editable
|
||||||
/* Make it workd */
|
/* Make it workd */
|
||||||
struct Meters(u32);
|
struct Meters(u32);
|
||||||
|
@ -41,7 +41,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
3. ππ The `newtype` idiom gives compile time guarantees that the right type of value is suplied to a program.
|
3. ππ The `newtype` idiom gives compile time guarantees that the right type of value is supplied to a program.
|
||||||
```rust,editable
|
```rust,editable
|
||||||
/* Make it work */
|
/* Make it work */
|
||||||
struct Years(i64);
|
struct Years(i64);
|
||||||
|
@ -61,7 +61,7 @@ impl Days {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// an age verification function that checks age in years, must be given a value of type Years.
|
// An age verification function that checks age in years, must be given a value of type Years.
|
||||||
fn old_enough(age: &Years) -> bool {
|
fn old_enough(age: &Years) -> bool {
|
||||||
age.0 >= 18
|
age.0 >= 18
|
||||||
}
|
}
|
||||||
|
@ -98,12 +98,12 @@ fn main() {
|
||||||
assert_eq!(format!("{}",d), "There are still 30 meters left");
|
assert_eq!(format!("{}",d), "There are still 30 meters left");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* implement calculate_distance */
|
/* Implement calculate_distance */
|
||||||
fn calculate_distance
|
fn calculate_distance
|
||||||
```
|
```
|
||||||
|
|
||||||
## Type alias
|
## Type alias
|
||||||
The most importance of type alias is to improve the readability of our codes.
|
Type alias is important to improve the readability of our code.
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
type Thunk = Box<dyn Fn() + Send + 'static>;
|
type Thunk = Box<dyn Fn() + Send + 'static>;
|
||||||
|
@ -150,7 +150,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
6. ππ There are a few preserved alias in Rust, one of which can be used in `impl` blocks.
|
6. ππ There are a few preserved aliases in Rust, one of which can be used in `impl` blocks.
|
||||||
```rust,editable
|
```rust,editable
|
||||||
enum VeryVerboseEnumOfThingsToDoWithNumbers {
|
enum VeryVerboseEnumOfThingsToDoWithNumbers {
|
||||||
Add,
|
Add,
|
||||||
|
@ -193,7 +193,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
9. ππ Trait is also a unsized type
|
9. ππ Trait is also an unsized type
|
||||||
```rust,editable
|
```rust,editable
|
||||||
/* Make it work in two ways */
|
/* Make it work in two ways */
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
|
|
Loadingβ¦
Reference in New Issue