-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgeneric_trait_bound.rs
75 lines (58 loc) · 1.16 KB
/
generic_trait_bound.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#![allow(unused)]
// Trait bound - specifies constraints on a generic type
trait A {}
trait B {}
trait C {}
impl A for u32 {}
impl B for u32 {}
impl C for u32 {}
impl A for i32 {}
// Trait bounds
// x must implement A
fn c<T: A>(x: T) {}
// Multiple trait bounds
// x must implement A and B
fn m<T: A + B>(x: T) {}
// Where clause
// x must implement A and B
// y must implement B and C
fn w<T, U>(x: T, y: U)
where
T: A + B,
U: B + C,
{
}
// Difference between impl trait syntax and trait bounds
// x and y can be different types
fn f(x: impl A, y: impl A) {}
// x and y must be the same type
fn g<T: A>(x: T, y: T) {}
// x and y can be different types
fn h<T: A, U: A>(x: T, y: U) {}
fn main() {
let x: u32 = 1;
let y: i32 = 1;
let z: f32 = 1.0;
c(x);
// This won't compile
// f32 doesn't implement A
// c(z);
m(x);
// This won't compile
// i32 doesn't implement B
// m(y);
w(x, x);
// This won't compile
// i32 doesn't implement B and C
// w(x, y);
f(x, x);
f(y, y);
f(x, y);
g(x, x);
g(y, y);
// This won't compile
// g(x, y);
h(x, x);
h(y, y);
h(x, y);
}