I have such code:
fn main() {
pub struct Book {
pub title: String,
pub author: String,
};
impl Book {
fn print_title(book: Book) {
println!("{}", book.title);
}
}
let books: [Book; 2] = [
Book {
title: String::from("Best Served Cold"),
author: String::from("Joe Abercrombie"),
},
Book {
title: String::from("The Lord of the Rings"),
author: String::from("John Tolkien"),
},
];
for book in books.into_iter() {
Book::print_title(book);
}
}
What I'm trying to do is to pass each book
value into a related function (I know it probably could be done some dumber or smarter way, yet I need to use related function). The problem is I'm getting this error:
expected struct models::NewBook, found reference
If I do Book::print_title(*book);
then I get
cannot move out of `*book` which is behind a shared reference
So the question is how can I pass the value of a book
to the related function print_title
?
books
array won't be ever reused and I won't ever need book
values if that matters.