■ trait 키워드를 사용해 기본 구현 메소드를 갖는 트레잇을 만드는 방법을 보여준다.
▶ 예제 코드 (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 |
pub trait Summarizable { fn summary(&self) -> String { return String::from("(Read more...)"); } } pub struct News { pub headline : String, pub location : String, pub author : String, pub content : String } impl Summarizable for News {} fn main() { let article = News { headline : String::from("Penguins win the Stanley Cup Championship!"), location : String::from("Pittsburgh, PA, USA"), author : String::from("Iceburgh"), content : String::from("The Pittsburgh Penguins once again are the best hockey team in the NHL.") }; println!("New article available! {}", article.summary()); } /* New article available! (Read more...) */ |