-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgeneric_bounds.ko
More file actions
47 lines (39 loc) · 914 Bytes
/
generic_bounds.ko
File metadata and controls
47 lines (39 loc) · 914 Bytes
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
module generic_bounds {
meta {
purpose: "Demonstrates trait bounds on generic type parameters"
version: "1.0.0"
}
trait Printable {
fn display(self) -> String
}
trait Comparable {
fn compare(self) -> Int
}
struct User {
name: String,
age: Int,
}
impl Printable for User {
fn display(self) -> String {
return "User"
}
}
impl Comparable for User {
fn compare(self) -> Int {
return 0
}
}
fn show<T: Printable>(item: T) -> Int {
return 0
}
fn process<T: Printable + Comparable>(item: T) -> Int {
return 0
}
fn main() -> Int {
let u: User = User { name: "Alice", age: 30 }
let r1: Int = show(u)
let u2: User = User { name: "Bob", age: 25 }
let r2: Int = process(u2)
return 0
}
}