Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add_book method of library throws cannot borrow as mutable without declaring library as mut #99

Closed
rhapsodyai opened this issue Jan 3, 2023 · 1 comment
Assignees

Comments

@rhapsodyai
Copy link
Contributor

rhapsodyai commented Jan 3, 2023

Hi I was having an issue with the exercise at the end of day 1. Writing it as it is shown in the solution

   fn add_book(&mut self, book: Book) {
        self.books.push(book)
    }

will throw an error stating "cannot borrow library as mutable, as it is not declared as mutable". Adding mut to the library declaration seems to make it compile, though my impression was that we should not have to change the code in main. Or, perhaps there is something I am overlooking here? I am using rustc version 1.66.0.

@mgeisler
Copy link
Collaborator

mgeisler commented Jan 4, 2023

Hey @rhapsodyai,

Adding mut to the library declaration seems to make it compile, though my impression was that we should not have to change the code in main

That is indeed what you need to do to solve this exercise 😄 There are actually solutions for most exercises — though they're not linked from the exercise page itself to avoid spoiling it for people.

Would it help if we move up the comment found in main? Right now it says

fn main() {
    // This shows the desired behavior. Uncomment the code below and
    // implement the missing methods. You will need to update the
    // method signatures, including the "self" parameter!
    let library = Library::new();

We could move the part about updating the method signatures to just below impl Library.

Would you be up for sending a PR wih that change? Please add Fixes: #99 to the commit message if you do so that this issue can be auto-closed.

@mgeisler mgeisler self-assigned this Jan 4, 2023
@rhapsodyai rhapsodyai mentioned this issue Jan 5, 2023
mgeisler added a commit that referenced this issue Jan 5, 2023
NoahDragon pushed a commit to wnghl/comprehensive-rust that referenced this issue Jul 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants