struggling to get this library example compiled :-\

https://google.github.io/comprehensive-rust/exercises/day-1/book-library.html
This commit is contained in:
Vincent Batts 2023-01-21 10:42:02 -05:00
parent 3c73ea8d73
commit 77cefee5fb
72 changed files with 96 additions and 19 deletions

View File

@ -1,20 +1,89 @@
fn main() {
let mut vec = vec![10, 20];
#![allow(unused_variables, dead_code)]
println!("{vec:?}");
vec.push(30);
println!("{vec:?}");
println!("middle value: {}", vec[vec.len() / 2]);
println!("vec.iter()");
for item in vec.iter() {
println!(" - {item}");
}
let a = vec.pop();
println!("popped out {a:?}");
println!("{vec:?}");
struct Library<'a> {
books: Vec<&'a Book>,
}
struct Book {
title: String,
year: u16,
}
impl Book {
fn new(title: &str, year: u16) -> Book {
Book {
title: String::from(title),
year,
}
}
}
impl std::fmt::Display for Book {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} ({})", self.title, self.year)
}
}
impl Library<'_> {
fn new() -> Library<'static> {
return Library { books: vec![] };
}
fn len(&self) -> usize {
return self.books.len();
}
fn is_empty(&self) -> bool {
return self.books.len() == 0;
}
fn add_book(&mut self, book: &Book) {
self.books.push(book);
}
fn print_books(&self) {
if self.is_empty() {
println!("no books");
return;
}
for book in self.books.iter() {
println!("\"{}\" ({})", book.title, book.year);
}
}
fn oldest_book(&self) -> Option<&'static Book> {
let mut is_set: u8 = 0;
let mut oldest: &Book;
for book in self.books.iter() {
if is_set == 0 {
oldest = book;
is_set = 1;
continue;
}
if oldest.year > book.year {
oldest = book;
}
}
return Some(oldest);
}
}
fn main() {
let library: &mut Library<'_> = &mut Library::new();
println!("our Library is empty: {}", library.is_empty());
library.add_book(&Book::new("Lord of the Rings", 1954));
library.add_book(&Book::new("Alice's Adventures in Wonderland", 1865));
library.print_books();
match library.oldest_book() {
Some(book) => println!("My oldest book is {book}"),
None => println!("my library is empty!"),
}
println!("Our library has {} Books", library.len());
}

View File

@ -1 +0,0 @@
7cbe46f1ccde8504

View File

@ -0,0 +1,9 @@
{"message":"lifetimes cannot start with a number","code":null,"level":"error","spans":[{"file_name":"src/main.rs","byte_start":734,"byte_end":736,"line_start":40,"line_end":40,"column_start":27,"column_end":29,"is_primary":true,"text":[{"text":" fn add_book(&mut self<'2>, book: &Book<'1>) {","highlight_start":27,"highlight_end":29}],"label":null,"suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[{"message":"add `'` to close the char literal","code":null,"level":"help","spans":[{"file_name":"src/main.rs","byte_start":736,"byte_end":736,"line_start":40,"line_end":40,"column_start":29,"column_end":29,"is_primary":true,"text":[{"text":" fn add_book(&mut self<'2>, book: &Book<'1>) {","highlight_start":29,"highlight_end":29}],"label":null,"suggested_replacement":"'","suggestion_applicability":"MaybeIncorrect","expansion":null}],"children":[],"rendered":null}],"rendered":"\u001b[0m\u001b[1m\u001b[38;5;9merror\u001b[0m\u001b[0m\u001b[1m: lifetimes cannot start with a number\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m--> \u001b[0m\u001b[0msrc/main.rs:40:27\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\n\u001b[0m\u001b[1m\u001b[38;5;12m40\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\u001b[0m \u001b[0m\u001b[0m fn add_book(&mut self<'2>, book: &Book<'1>) {\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m| \u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;9m^^\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\n\u001b[0m\u001b[1m\u001b[38;5;14mhelp\u001b[0m\u001b[0m: add `'` to close the char literal\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\n\u001b[0m\u001b[1m\u001b[38;5;12m40\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m| \u001b[0m\u001b[0m fn add_book(&mut self<'2\u001b[0m\u001b[0m\u001b[38;5;10m'\u001b[0m\u001b[0m>, book: &Book<'1>) {\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[38;5;10m+\u001b[0m\n\n"}
{"message":"expected one of `:` or `|`, found `>`","code":null,"level":"error","spans":[{"file_name":"src/main.rs","byte_start":736,"byte_end":737,"line_start":40,"line_end":40,"column_start":29,"column_end":30,"is_primary":true,"text":[{"text":" fn add_book(&mut self<'2>, book: &Book<'1>) {","highlight_start":29,"highlight_end":30}],"label":"expected one of `:` or `|`","suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[],"rendered":"\u001b[0m\u001b[1m\u001b[38;5;9merror\u001b[0m\u001b[0m\u001b[1m: expected one of `:` or `|`, found `>`\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m--> \u001b[0m\u001b[0msrc/main.rs:40:29\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\n\u001b[0m\u001b[1m\u001b[38;5;12m40\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\u001b[0m \u001b[0m\u001b[0m fn add_book(&mut self<'2>, book: &Book<'1>) {\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m| \u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;9m^\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;9mexpected one of `:` or `|`\u001b[0m\n\n"}
{"message":"expected one of `)` or `,`, found `<`","code":null,"level":"error","spans":[{"file_name":"src/main.rs","byte_start":733,"byte_end":734,"line_start":40,"line_end":40,"column_start":26,"column_end":27,"is_primary":true,"text":[{"text":" fn add_book(&mut self<'2>, book: &Book<'1>) {","highlight_start":26,"highlight_end":27}],"label":"expected one of `)` or `,`","suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[{"message":"missing `,`","code":null,"level":"help","spans":[{"file_name":"src/main.rs","byte_start":733,"byte_end":733,"line_start":40,"line_end":40,"column_start":26,"column_end":26,"is_primary":true,"text":[{"text":" fn add_book(&mut self<'2>, book: &Book<'1>) {","highlight_start":26,"highlight_end":26}],"label":null,"suggested_replacement":",","suggestion_applicability":"MaybeIncorrect","expansion":null}],"children":[],"rendered":null}],"rendered":"\u001b[0m\u001b[1m\u001b[38;5;9merror\u001b[0m\u001b[0m\u001b[1m: expected one of `)` or `,`, found `<`\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m--> \u001b[0m\u001b[0msrc/main.rs:40:26\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\n\u001b[0m\u001b[1m\u001b[38;5;12m40\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\u001b[0m \u001b[0m\u001b[0m fn add_book(&mut self<'2>, book: &Book<'1>) {\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m| \u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;9m^\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m| \u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m| \u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;9mexpected one of `)` or `,`\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m| \u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12mhelp: missing `,`\u001b[0m\n\n"}
{"message":"use of undeclared lifetime name `'1`","code":{"code":"E0261","explanation":"An undeclared lifetime was used.\n\nErroneous code example:\n\n```compile_fail,E0261\n// error, use of undeclared lifetime name `'a`\nfn foo(x: &'a str) { }\n\nstruct Foo {\n // error, use of undeclared lifetime name `'a`\n x: &'a str,\n}\n```\n\nThese can be fixed by declaring lifetime parameters:\n\n```\nstruct Foo<'a> {\n x: &'a str,\n}\n\nfn foo<'a>(x: &'a str) {}\n```\n\nImpl blocks declare lifetime parameters separately. You need to add lifetime\nparameters to an impl block if you're implementing a type that has a lifetime\nparameter of its own.\nFor example:\n\n```compile_fail,E0261\nstruct Foo<'a> {\n x: &'a str,\n}\n\n// error, use of undeclared lifetime name `'a`\nimpl Foo<'a> {\n fn foo<'a>(x: &'a str) {}\n}\n```\n\nThis is fixed by declaring the impl block like this:\n\n```\nstruct Foo<'a> {\n x: &'a str,\n}\n\n// correct\nimpl<'a> Foo<'a> {\n fn foo(x: &'a str) {}\n}\n```\n"},"level":"error","spans":[{"file_name":"src/main.rs","byte_start":751,"byte_end":753,"line_start":40,"line_end":40,"column_start":44,"column_end":46,"is_primary":true,"text":[{"text":" fn add_book(&mut self<'2>, book: &Book<'1>) {","highlight_start":44,"highlight_end":46}],"label":"undeclared lifetime","suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[{"message":"consider introducing lifetime `'1` here","code":null,"level":"help","spans":[{"file_name":"src/main.rs","byte_start":723,"byte_end":723,"line_start":40,"line_end":40,"column_start":16,"column_end":16,"is_primary":true,"text":[{"text":" fn add_book(&mut self<'2>, book: &Book<'1>) {","highlight_start":16,"highlight_end":16}],"label":null,"suggested_replacement":"<'1>","suggestion_applicability":"MaybeIncorrect","expansion":null}],"children":[],"rendered":null},{"message":"consider introducing lifetime `'1` here","code":null,"level":"help","spans":[{"file_name":"src/main.rs","byte_start":462,"byte_end":462,"line_start":27,"line_end":27,"column_start":5,"column_end":5,"is_primary":true,"text":[{"text":"impl Library<'_> {","highlight_start":5,"highlight_end":5}],"label":null,"suggested_replacement":"<'1>","suggestion_applicability":"MaybeIncorrect","expansion":null}],"children":[],"rendered":null}],"rendered":"\u001b[0m\u001b[1m\u001b[38;5;9merror[E0261]\u001b[0m\u001b[0m\u001b[1m: use of undeclared lifetime name `'1`\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m--> \u001b[0m\u001b[0msrc/main.rs:40:44\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\n\u001b[0m\u001b[1m\u001b[38;5;12m40\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\u001b[0m \u001b[0m\u001b[0m fn add_book(&mut self<'2>, book: &Book<'1>) {\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m| \u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;9m^^\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;9mundeclared lifetime\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\n\u001b[0m\u001b[1m\u001b[38;5;14mhelp\u001b[0m\u001b[0m: consider introducing lifetime `'1` here\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\n\u001b[0m\u001b[1m\u001b[38;5;12m40\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m| \u001b[0m\u001b[0m fn add_book\u001b[0m\u001b[0m\u001b[38;5;10m<'1>\u001b[0m\u001b[0m(&mut self<'2>, book: &Book<'1>) {\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[38;5;10m++++\u001b[0m\n\u001b[0m\u001b[1m\u001b[38;5;14mhelp\u001b[0m\u001b[0m: consider introducing lifetime `'1` here\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\n\u001b[0m\u001b[1m\u001b[38;5;12m27\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m| \u001b[0m\u001b[0mimpl\u001b[0m\u001b[0m\u001b[38;5;10m<'1>\u001b[0m\u001b[0m Library<'_> {\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[38;5;10m++++\u001b[0m\n\n"}
{"message":"this struct takes 0 lifetime arguments but 1 lifetime argument was supplied","code":{"code":"E0107","explanation":"An incorrect number of generic arguments was provided.\n\nErroneous code example:\n\n```compile_fail,E0107\nstruct Foo<T> { x: T }\n\nstruct Bar { x: Foo } // error: wrong number of type arguments:\n // expected 1, found 0\nstruct Baz<S, T> { x: Foo<S, T> } // error: wrong number of type arguments:\n // expected 1, found 2\n\nfn foo<T, U>(x: T, y: U) {}\nfn f() {}\n\nfn main() {\n let x: bool = true;\n foo::<bool>(x); // error: wrong number of type arguments:\n // expected 2, found 1\n foo::<bool, i32, i32>(x, 2, 4); // error: wrong number of type arguments:\n // expected 2, found 3\n f::<'static>(); // error: wrong number of lifetime arguments\n // expected 0, found 1\n}\n```\n\nWhen using/declaring an item with generic arguments, you must provide the exact\nsame number:\n\n```\nstruct Foo<T> { x: T }\n\nstruct Bar<T> { x: Foo<T> } // ok!\nstruct Baz<S, T> { x: Foo<S>, y: Foo<T> } // ok!\n\nfn foo<T, U>(x: T, y: U) {}\nfn f() {}\n\nfn main() {\n let x: bool = true;\n foo::<bool, u32>(x, 12); // ok!\n f(); // ok!\n}\n```\n"},"level":"error","spans":[{"file_name":"src/main.rs","byte_start":746,"byte_end":750,"line_start":40,"line_end":40,"column_start":39,"column_end":43,"is_primary":true,"text":[{"text":" fn add_book(&mut self<'2>, book: &Book<'1>) {","highlight_start":39,"highlight_end":43}],"label":"expected 0 lifetime arguments","suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[{"message":"struct defined here, with 0 lifetime parameters","code":null,"level":"note","spans":[{"file_name":"src/main.rs","byte_start":97,"byte_end":101,"line_start":7,"line_end":7,"column_start":8,"column_end":12,"is_primary":true,"text":[{"text":"struct Book {","highlight_start":8,"highlight_end":12}],"label":null,"suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[],"rendered":null},{"message":"remove these generics","code":null,"level":"help","spans":[{"file_name":"src/main.rs","byte_start":750,"byte_end":754,"line_start":40,"line_end":40,"column_start":43,"column_end":47,"is_primary":true,"text":[{"text":" fn add_book(&mut self<'2>, book: &Book<'1>) {","highlight_start":43,"highlight_end":47}],"label":null,"suggested_replacement":"","suggestion_applicability":"MaybeIncorrect","expansion":null}],"children":[],"rendered":null}],"rendered":"\u001b[0m\u001b[1m\u001b[38;5;9merror[E0107]\u001b[0m\u001b[0m\u001b[1m: this struct takes 0 lifetime arguments but 1 lifetime argument was supplied\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m--> \u001b[0m\u001b[0msrc/main.rs:40:39\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\n\u001b[0m\u001b[1m\u001b[38;5;12m40\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\u001b[0m \u001b[0m\u001b[0m fn add_book(&mut self<'2>, book: &Book<'1>) {\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m| \u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;9m^^^^\u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m----\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12mhelp: remove these generics\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m| \u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;9m|\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m| \u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;9mexpected 0 lifetime arguments\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\n\u001b[0m\u001b[1m\u001b[38;5;10mnote\u001b[0m\u001b[0m: struct defined here, with 0 lifetime parameters\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m--> \u001b[0m\u001b[0msrc/main.rs:7:8\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\n\u001b[0m\u001b[1m\u001b[38;5;12m7\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\u001b[0m \u001b[0m\u001b[0mstruct Book {\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m| \u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;10m^^^^\u001b[0m\n\n"}
{"message":"lifetimes cannot start with a number","code":null,"level":"error","spans":[{"file_name":"src/main.rs","byte_start":751,"byte_end":753,"line_start":40,"line_end":40,"column_start":44,"column_end":46,"is_primary":true,"text":[{"text":" fn add_book(&mut self<'2>, book: &Book<'1>) {","highlight_start":44,"highlight_end":46}],"label":null,"suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[],"rendered":"\u001b[0m\u001b[1m\u001b[38;5;9merror\u001b[0m\u001b[0m\u001b[1m: lifetimes cannot start with a number\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m--> \u001b[0m\u001b[0msrc/main.rs:40:44\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\n\u001b[0m\u001b[1m\u001b[38;5;12m40\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\u001b[0m \u001b[0m\u001b[0m fn add_book(&mut self<'2>, book: &Book<'1>) {\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m| \u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;9m^^\u001b[0m\n\n"}
{"message":"aborting due to 6 previous errors","code":null,"level":"error","spans":[],"children":[],"rendered":"\u001b[0m\u001b[1m\u001b[38;5;9merror\u001b[0m\u001b[0m\u001b[1m: aborting due to 6 previous errors\u001b[0m\n\n"}
{"message":"Some errors have detailed explanations: E0107, E0261.","code":null,"level":"failure-note","spans":[],"children":[],"rendered":"\u001b[0m\u001b[1mSome errors have detailed explanations: E0107, E0261.\u001b[0m\n"}
{"message":"For more information about an error, try `rustc --explain E0107`.","code":null,"level":"failure-note","spans":[],"children":[],"rendered":"\u001b[0m\u001b[1mFor more information about an error, try `rustc --explain E0107`.\u001b[0m\n"}

Binary file not shown.