-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtraits.rs
More file actions
46 lines (36 loc) · 946 Bytes
/
Copy pathtraits.rs
File metadata and controls
46 lines (36 loc) · 946 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
//Traits
trait Summary{
fn summarize(&self) -> String{
return String::from("some output from summarize");
}
}
trait Fix{
fn fix(&self) -> String{
return String::from("some output from fix");
}
}
struct User{
name: String,
age: u32,
}
impl Summary for User{
// fn summarize(&self) -> String {
// return format!("Name is {} and age is {}",self.name,self.age);
// }
}
impl Fix for User{}
impl Summary for String{}
//traits as parameters
//notify only accepts input that implement summary
fn notify<T: Summary + Fix>(u: T){
println!("Anyone who implements Summary and Fix: {} {}",u.summarize(),u.fix());
}
fn main(){
let user = User{
name : String::from("harkirat"),
age : 22,
};
notify(user);
//notify(String::from("Any String as I have implemented SUmmary for the String struct"));
//gives error as String only satisfies the summary trait.
}