Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Returning Types that Implement Traits

Chúng ta cũng có thể sử dụng impl Trait cho giá trị được trả về của function.

fn returns_summarizable() -> impl Summary {
    Tweet {
        username: String::from("horse_ebooks"),
        content: String::from("ahihi"),
        reply: false,
        retweet: false,
    }
}

Được đọc là: function returns_summarizable() trả về bất kỳ kiểu dữ liệu nào có impl Summary. Tuy nhiên bạn chỉ có thể return về hoặc Tweet hoặc NewsArticle do cách implement của compiler. Code sau sẽ có lỗi:

fn returns_summarizable(switch: bool) -> impl Summary {
    if switch { NewsArticle {} }
		else { Tweet {} }
}

Rust Book có một chương riêng để xử lý vấn đề này: Chapter 17: Using Trait Objects That Allow for Values of Different Types