The queue will hold more than books. An article or a single chapter can be queued too, and each needs an ID. In this checkpoint you combine today's ideas: one generic wrapper for any record, one trait for the behavior a report needs, and report functions that work for every record type with that behavior. The design question to get right is where the bound goes. The wrapper should accept anything; only the report needs titles.
What you will use
- A generic struct.
Tagged<T>pairs au32ID with a value of any typeT. - A trait of your own.
Titledeclaresfn title(&self) -> &str, andBookandArticleimplement it. - A trait bound on a function.
show<T: Title>borrows a&Tagged<T>and can calltitleon the value. - A slice of generic values.
show_alltakes&[Tagged<T>], as on Day 2, so it works with an array or a vector.
Plan before you type
- Declare
trait Titleand implement it forBookandArticle, returning&self.0. - Write
fn show<T: Title>(record: &Tagged<T>)that prints the ID and the title. - Write
fn show_all<T: Title>(records: &[Tagged<T>])that callsshowfor each record. - In
main, show the book and the article, then callshow_allon an array of two articles. - Build a
Tagged<u32>for a page count and print its fields directly, to confirm the wrapper has no bound.
Prove the bound is where it belongs
Add T: Title to the struct, struct Tagged<T: Title>, and run. The page-count tag must now fail with E0277, because u32 has no title. That is why the bound belongs on the functions. Then try show(&pages) with the original struct: it should fail the same way, at the call.
Mistakes worth checking for
error[E0599]: no method named title found:showhas a type parameter but noT: Titlebound.error[E0277]: the trait bound u32: Title is not satisfiedon the page-count tag: the bound was put on the struct instead of the function.show_allcannot take an array: its parameter is&Vec<Tagged<T>>instead of the slice&[Tagged<T>].- Mixing a book and an article in one array fails with E0308: an array holds one type, and
Tagged<Book>andTagged<Article>are different types. Show them separately.
Your turn
Give Book and Article a common Title trait. Write show for one tagged record and show_all for a slice of them. Print #7 Rust Patterns and #9 Iterator Notes with show, then #10 Trait Basics and #11 Error Handling with show_all on an array of two articles. Finally build a Tagged<u32> with ID 7 and value 320 and print #7 has 320 pages.
struct Tagged<T> {
id: u32,
value: T,
}
struct Book(String);
struct Article(String);
fn main() {
let book = Tagged { id: 7, value: Book(String::from("Rust Patterns")) };
let article = Tagged { id: 9, value: Article(String::from("Iterator Notes")) };
// Show both records through one function, then a slice of articles.
}Hint 1: the idea
Only the reports need access to titles. Tagged can stay generic over any T, and each report function carries the bound itself.
Hint 2: a first step
Give show a parameter of type &Tagged<T> and require T: Title. In show_all, loop with for record in records and pass each record to show; it is already a reference.
Worked solution
struct Tagged<T> {
id: u32,
value: T,
}
trait Title {
fn title(&self) -> &str;
}
struct Book(String);
struct Article(String);
impl Title for Book {
fn title(&self) -> &str { &self.0 }
}
impl Title for Article {
fn title(&self) -> &str { &self.0 }
}
fn show<T: Title>(record: &Tagged<T>) {
println!("#{} {}", record.id, record.value.title());
}
fn show_all<T: Title>(records: &[Tagged<T>]) {
for record in records {
show(record);
}
}
fn main() {
let book = Tagged { id: 7, value: Book(String::from("Rust Patterns")) };
let article = Tagged { id: 9, value: Article(String::from("Iterator Notes")) };
show(&book);
show(&article);
let reading_list = [
Tagged { id: 10, value: Article(String::from("Trait Basics")) },
Tagged { id: 11, value: Article(String::from("Error Handling")) },
];
show_all(&reading_list);
let pages = Tagged { id: 7, value: 320 };
println!("#{} has {} pages", pages.id, pages.value);
}#7 Rust Patterns
#9 Iterator Notes
#10 Trait Basics
#11 Error Handling
#7 has 320 pagesThe bound belongs to the functions that use the behavior, so Tagged can still wrap a page count that has no title. show_all passes the bound on to show because both name T: Title. One array still holds a single record type; holding books and articles together needs a different tool, an enum of records, or a trait object as in the advanced course. Day 4 turns to collections: ordered maps and iterators.
Completion is saved in this browser only. It does not measure mastery.