add generic.md
This commit is contained in:
parent
7ea642691e
commit
a8f251fd2a
|
@ -0,0 +1,88 @@
|
|||
1.
|
||||
```rust
|
||||
struct A; // Concrete type `A`.
|
||||
struct S(A); // Concrete type `S`.
|
||||
struct SGen<T>(T); // Generic type `SGen`.
|
||||
|
||||
fn reg_fn(_s: S) {}
|
||||
|
||||
fn gen_spec_t(_s: SGen<A>) {}
|
||||
|
||||
fn gen_spec_i32(_s: SGen<i32>) {}
|
||||
|
||||
fn generic<T>(_s: SGen<T>) {}
|
||||
|
||||
fn main() {
|
||||
// Using the non-generic functions
|
||||
reg_fn(S(A)); // Concrete type.
|
||||
gen_spec_t(SGen(A)); // Implicitly specified type parameter `A`.
|
||||
gen_spec_i32(SGen(6)); // Implicitly specified type parameter `i32`.
|
||||
|
||||
// Explicitly specified type parameter `char` to `generic()`.
|
||||
generic::<char>(SGen('a'));
|
||||
|
||||
// Implicitly specified type parameter `char` to `generic()`.
|
||||
generic(SGen('c'));
|
||||
}
|
||||
```
|
||||
|
||||
2.
|
||||
```rust
|
||||
fn sum<T:std::ops::Add<Output = T>>(x: T, y: T) -> T {
|
||||
x + y
|
||||
}
|
||||
|
||||
fn main() {
|
||||
assert_eq!(5, sum(2i8, 3i8));
|
||||
assert_eq!(50, sum(20, 30));
|
||||
assert_eq!(2.46, sum(1.23, 1.23));
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
3.
|
||||
```rust
|
||||
struct Point<T> {
|
||||
x: T,
|
||||
y: T,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let integer = Point { x: 5, y: 10 };
|
||||
let float = Point { x: 1.0, y: 4.0 };
|
||||
}
|
||||
```
|
||||
|
||||
4.
|
||||
```rust
|
||||
// modify this struct to make the code work
|
||||
struct Point<T, U> {
|
||||
x: T,
|
||||
y: U,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// DON'T modify here
|
||||
let p = Point{x: 5, y : "hello".to_string()};
|
||||
}
|
||||
```
|
||||
|
||||
5.
|
||||
```rust
|
||||
struct Val<T> {
|
||||
val: T,
|
||||
}
|
||||
|
||||
impl<T> Val<T> {
|
||||
fn value(&self) -> &T {
|
||||
&self.val
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn main() {
|
||||
let x = Val{ val: 3.0 };
|
||||
let y = Val{ val: "hello".to_string()};
|
||||
println!("{}, {}", x.value(), y.value());
|
||||
}
|
||||
```
|
|
@ -22,7 +22,7 @@
|
|||
- [match, matches! and if let](pattern-match/match-iflet.md)
|
||||
- [Patterns](pattern-match/patterns.md)
|
||||
- [Method & Associated function](method.md)
|
||||
- [Generics and Traits todo](generics-traits/intro.md)
|
||||
- [Generics and Traits](generics-traits/intro.md)
|
||||
- [Generics](generics-traits/generics.md)
|
||||
- [Traits](generics-traits/traits.md)
|
||||
- [Trait Object](generics-traits/trait-object.md)
|
||||
|
|
|
@ -1 +1,98 @@
|
|||
# Generics
|
||||
|
||||
### Functions
|
||||
1. 🌟🌟🌟
|
||||
```rust,editable
|
||||
|
||||
// fill in the blanks to make it work
|
||||
struct A; // Concrete type `A`.
|
||||
struct S(A); // Concrete type `S`.
|
||||
struct SGen<T>(T); // Generic type `SGen`.
|
||||
|
||||
fn reg_fn(_s: S) {}
|
||||
|
||||
fn gen_spec_t(_s: SGen<A>) {}
|
||||
|
||||
fn gen_spec_i32(_s: SGen<i32>) {}
|
||||
|
||||
fn generic<T>(_s: SGen<T>) {}
|
||||
|
||||
fn main() {
|
||||
// Using the non-generic functions
|
||||
reg_fn(__); // Concrete type.
|
||||
gen_spec_t(__); // Implicitly specified type parameter `A`.
|
||||
gen_spec_i32(__); // Implicitly specified type parameter `i32`.
|
||||
|
||||
// Explicitly specified type parameter `char` to `generic()`.
|
||||
generic::<char>(__);
|
||||
|
||||
// Implicitly specified type parameter `char` to `generic()`.
|
||||
generic(__);
|
||||
}
|
||||
```
|
||||
|
||||
2. 🌟🌟 A function call with explicitly specified type parameters looks like: `fun::<A, B, ...>()`.
|
||||
```rust,editable
|
||||
|
||||
// implement a generic function
|
||||
fn sum
|
||||
|
||||
fn main() {
|
||||
assert_eq!(5, sum(2i8, 3i8));
|
||||
assert_eq!(50, sum(20, 30));
|
||||
assert_eq!(2.46, sum(1.23, 1.23));
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### Struct and `impl`
|
||||
|
||||
3. 🌟
|
||||
```rust,editable
|
||||
|
||||
// implement struct Point to make it work
|
||||
|
||||
|
||||
fn main() {
|
||||
let integer = Point { x: 5, y: 10 };
|
||||
let float = Point { x: 1.0, y: 4.0 };
|
||||
}
|
||||
```
|
||||
|
||||
4. 🌟🌟
|
||||
```rust,editable
|
||||
|
||||
// modify this struct to make the code work
|
||||
struct Point<T> {
|
||||
x: T,
|
||||
y: T,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// DON'T modify here
|
||||
let p = Point{x: 5, y : "hello".to_string()};
|
||||
}
|
||||
```
|
||||
|
||||
5. 🌟🌟
|
||||
```rust,editable
|
||||
|
||||
// add generic for Val to make the code work, DON'T modify the code in `main`
|
||||
struct Val {
|
||||
val: f64,
|
||||
}
|
||||
|
||||
impl Val {
|
||||
fn value(&self) -> &f64 {
|
||||
&self.val
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn main() {
|
||||
let x = Val{ val: 3.0 };
|
||||
let y = Val{ val: "hello".to_string()};
|
||||
println!("{}, {}", x.value(), y.value());
|
||||
}
|
||||
```
|
||||
> You can find the solutions [here](https://github.com/sunface/rust-by-practice)(under the solutions path), but only use it when you need it
|
|
@ -1 +1,6 @@
|
|||
# Generics and Traits
|
||||
Learning resources:
|
||||
- English: [Rust Book 10.1, 10.2](https://doc.rust-lang.org/book/ch10-00-generics.html)
|
||||
- 简体中文: [Rust语言圣经 - 模式匹配](https://course.rs/basic/trait/intro.html)
|
||||
|
||||
|
||||
|
|
|
@ -1 +1,8 @@
|
|||
# Traits
|
||||
|
||||
|
||||
|
||||
fn main() {
|
||||
assert_eq!(5, sum(2i8, 3u8));
|
||||
assert_eq!(50, sum(20, 30.1));
|
||||
}
|
|
@ -259,4 +259,7 @@ fn main() {
|
|||
|
||||
## Practice
|
||||
|
||||
@todo
|
||||
@todo
|
||||
|
||||
|
||||
> You can find the solutions [here](https://github.com/sunface/rust-by-practice)(under the solutions path), but only use it when you need it
|
Loading…
Reference in New Issue