2022-02-23 00:49:02 -06:00
|
|
|
# Generics
|
2022-03-03 03:13:21 -06:00
|
|
|
|
|
|
|
### Functions
|
|
|
|
1. πππ
|
|
|
|
```rust,editable
|
|
|
|
|
2022-05-20 09:24:19 -05:00
|
|
|
// Fill in the blanks to make it work
|
2022-03-03 03:13:21 -06:00
|
|
|
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(__);
|
2022-03-05 00:48:43 -06:00
|
|
|
|
2022-05-20 09:24:19 -05:00
|
|
|
println!("Success!");
|
2022-03-03 03:13:21 -06:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
2. ππ A function call with explicitly specified type parameters looks like: `fun::<A, B, ...>()`.
|
|
|
|
```rust,editable
|
|
|
|
|
2022-05-20 09:24:19 -05:00
|
|
|
// Implement the generic function below.
|
2022-03-03 03:13:21 -06:00
|
|
|
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));
|
2022-03-05 00:48:43 -06:00
|
|
|
|
2022-05-20 09:24:19 -05:00
|
|
|
println!("Success!");
|
2022-03-03 03:13:21 -06:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### Struct and `impl`
|
|
|
|
|
|
|
|
3. π
|
|
|
|
```rust,editable
|
|
|
|
|
2022-05-20 09:24:19 -05:00
|
|
|
// Implement struct Point to make it work.
|
2022-03-03 03:13:21 -06:00
|
|
|
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let integer = Point { x: 5, y: 10 };
|
|
|
|
let float = Point { x: 1.0, y: 4.0 };
|
2022-03-05 00:48:43 -06:00
|
|
|
|
2022-05-20 09:24:19 -05:00
|
|
|
println!("Success!");
|
2022-03-03 03:13:21 -06:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
4. ππ
|
|
|
|
```rust,editable
|
|
|
|
|
2022-05-20 09:24:19 -05:00
|
|
|
// Modify this struct to make the code work
|
2022-03-03 03:13:21 -06:00
|
|
|
struct Point<T> {
|
|
|
|
x: T,
|
|
|
|
y: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2022-05-20 09:24:19 -05:00
|
|
|
// DON'T modify this code.
|
2022-03-03 03:13:21 -06:00
|
|
|
let p = Point{x: 5, y : "hello".to_string()};
|
2022-03-05 00:48:43 -06:00
|
|
|
|
2022-05-20 09:24:19 -05:00
|
|
|
println!("Success!");
|
2022-03-03 03:13:21 -06:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
5. ππ
|
|
|
|
```rust,editable
|
|
|
|
|
2022-05-20 09:24:19 -05:00
|
|
|
// Add generic for Val to make the code work, DON'T modify the code in `main`.
|
2022-03-03 03:13:21 -06:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
```
|
2022-03-03 20:54:59 -06:00
|
|
|
|
|
|
|
### Method
|
|
|
|
6. πππ
|
|
|
|
|
|
|
|
```rust,editable
|
|
|
|
struct Point<T, U> {
|
|
|
|
x: T,
|
|
|
|
y: U,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, U> Point<T, U> {
|
2022-05-20 09:24:19 -05:00
|
|
|
// Implement mixup to make it work, DON'T modify other code.
|
2022-03-03 20:54:59 -06:00
|
|
|
fn mixup
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let p1 = Point { x: 5, y: 10 };
|
|
|
|
let p2 = Point { x: "Hello", y: 'δΈ'};
|
|
|
|
|
|
|
|
let p3 = p1.mixup(p2);
|
|
|
|
|
|
|
|
assert_eq!(p3.x, 5);
|
|
|
|
assert_eq!(p3.y, 'δΈ');
|
2022-03-05 00:48:43 -06:00
|
|
|
|
2022-05-20 09:24:19 -05:00
|
|
|
println!("Success!");
|
2022-03-03 20:54:59 -06:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
7. ππ
|
|
|
|
```rust,editable
|
|
|
|
|
2022-05-20 09:24:19 -05:00
|
|
|
// Fix the errors to make the code work.
|
2022-03-03 20:54:59 -06:00
|
|
|
struct Point<T> {
|
|
|
|
x: T,
|
|
|
|
y: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Point<f32> {
|
|
|
|
fn distance_from_origin(&self) -> f32 {
|
|
|
|
(self.x.powi(2) + self.y.powi(2)).sqrt()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let p = Point{x: 5, y: 10};
|
2022-05-20 09:24:19 -05:00
|
|
|
println!("{}",p.distance_from_origin());
|
2022-03-03 20:54:59 -06:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
> 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
|
|
|
|
|