File tree Expand file tree Collapse file tree 1 file changed +13
-9
lines changed Expand file tree Collapse file tree 1 file changed +13
-9
lines changed Original file line number Diff line number Diff line change @@ -4,39 +4,43 @@ Similar to functions, implementations require care to remain generic.
4
4
5
5
``` rust
6
6
struct S ; // Concrete type `S`
7
- struct GenericVal <T >(T , ); // Generic type `GenericVal`
7
+ struct GenericVal <T >(T ); // Generic type `GenericVal`
8
8
9
9
// impl of GenericVal where we explicitly specify type parameters:
10
10
impl GenericVal <f32 > {} // Specify `f32`
11
11
impl GenericVal <S > {} // Specify `S` as defined above
12
12
13
13
// `<T>` Must precede the type to remain generic
14
- impl <T > GenericVal <T > {}
14
+ impl <T > GenericVal <T > {}
15
15
```
16
16
17
17
``` rust,editable
18
18
struct Val {
19
- val: f64
19
+ val: f64,
20
20
}
21
21
22
- struct GenVal<T>{
23
- gen_val: T
22
+ struct GenVal<T> {
23
+ gen_val: T,
24
24
}
25
25
26
26
// impl of Val
27
27
impl Val {
28
- fn value(&self) -> &f64 { &self.val }
28
+ fn value(&self) -> &f64 {
29
+ &self.val
30
+ }
29
31
}
30
32
31
33
// impl of GenVal for a generic type `T`
32
- impl <T> GenVal<T> {
33
- fn value(&self) -> &T { &self.gen_val }
34
+ impl<T> GenVal<T> {
35
+ fn value(&self) -> &T {
36
+ &self.gen_val
37
+ }
34
38
}
35
39
36
40
fn main() {
37
41
let x = Val { val: 3.0 };
38
42
let y = GenVal { gen_val: 3i32 };
39
-
43
+
40
44
println!("{}, {}", x.value(), y.value());
41
45
}
42
46
```
You can’t perform that action at this time.
0 commit comments