trait, lifetime
pub trait Summary { fn summarize(&self) -> String; }
pub struct Tweet { pub username: String, pub content: String, pub reply: bool, pub retweet: bool, } impl Summary for Tweet { fn summarize(&self) -> String { format!("{}: {}", self.username, self.content) } }
pub trait Summary { fn summarize(&self) -> String { String::from("(Read more...)") } }
println!("New article available! {}", article.summarize());
pub fn notify(item: &impl Summary) { println!("Breaking news! {}", item.summarize()); }
pub fn notify<T: Summary>(item: &T) { println!("Breaking news! {}", item.summarize()); }
pub fn notify(item1: &impl Summary, item2: &impl Summary) {
↓
pub fn notify<T: Summary>(item1: &T, item2: &T) {
pub fn notify(item: &(impl Summary + Display)) {
or
pub fn notify<T: Summary + Display>(item: &T) {
pub fn notify<T>(item: &T) where T: Summary + Display {
fn returns_summarizable() -> impl Summary { Tweet { ... } }
fn returns_summarizable(switch: bool) -> impl Summary { if switch { NewsArticle { ... } Tweet { ... } } }
impl<T> Pair<T> { fn new(x: T, y: T) -> Self { Self { x, y } } } impl<T: Display + PartialOrd> Pair<T> { fn cmp_display(&self) { if self.x >= self.y { println!("The largest member is x = {}", self.x); } else { println!("The largest member is y = {}", self.y); } } }
let r; { let x = 5; r = &x; } println!("r: {}", r);
fn main() { let r; // ---------+-- 'a // | { // | let x = 5; // -+-- 'b | r = &x; // | | } // -+ | // | println!("r: {}", r); // | } // ---------+
let string1 = String::from("abcd"); let string2 = "xyz"; let result = longest(string1.as_str(), string2);
fn longest(x: &str, y: &str) -> &str { if x.len() > y.len() { x } else { y } }
&i32 // 참조자 &'a i32 // 명시적인 라이프타임이 있는 참조자 &'a mut i32 // 명시적인 라이프타임이 있는 가변 참조자
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { if x.len() > y.len() { x } else { y } }
let string1 = String::from("long string is long"); let result; { let string2 = String::from("xyz"); let result = longest(string1.as_str(), string2.as_str()); } println!("The longest string is {}", result);
let string2 = String::from("xyz"); ------- binding `string2` declared here result = longest(string1.as_str(), string2.as_str()); ^^^^^^^^^^^^^^^^ borrowed } - `string2` dropped here while still borrowed println!("The longest string is {}", result); ------ borrow later used here
fn longest<'a>(x: &'a str, y: &str) -> &'a str { x }
struct ImportantExcerpt<'a> { part: &'a str, }
fn first_word(s: &str) -> &str { let bytes = s.as_bytes(); for (i, &item) in bytes.iter().enumerate() { if item == b' ' { return &s[0..i]; } } &s[..] }
fn longest<'a, 'b>(x: &'a str, y: &'b str) -> &str {
impl<'a> ImportantExcerpt<'a> { fn announce_and_return_part(&self, announcement: &str) -> &str { println!("Attention please: {}", announcement); self.part } }
let s: &'static str = "I have a static lifetime.";
use std::fmt::Display; fn longest_with_an_announcement<'a, T>( x: &'a str, y: &'a str, ann: T, ) -> &'a str where T: Display, { println!("Announcement! {}", ann); if x.len() > y.len() { x } else { y } }
제네릭 타입 매개변수, 트레이트 바운드, 라이프타임 문법 모두 사용한 코드