Enums troubles Others(String)

First I'm reading the book Learn Rust in a month of Lunches. So far so good but I wanted a little more explanation of enums.

So I watched the video here: https://youtu.be/-nFDSDE_2Nw?t=404 called ENUMS are at the HEART of Rust. At 6:44 in the author goes over using user text input. I followed along but keep running into a compile error:

typeerror: expected one of `)`, `,`, `@`, or `|`, found `:`
 --> testenum-NOTWORKING.rs:9:26
  |
9 |         Book::Other(title: &String) => title.into(),
  |                          ^ expected one of `)`, `,`, `@`, or `|`

error: aborting due to 1 previous error

So I simplified the code down to its bare bones trying to figure out what I messed up. Show here:

type or paste code enum Book {
    // use a text input
    Other(String),
}

fn display(book: &Book) -> String {
    match book { 
        //Book::Other(title: &String()) => title.into(),
        Book::Other(title: &String) => title.into(),
        //Book::Other(title: &string) -> title.into(),
    }
}


fn main() {
    let yourFavBook: Book = Book::Other("Atomics and Locks".into());

    println!("Your favorite book is: '{}'", display(&yourFavBook));
}here

But I can't seem to find anything online to help me figure out what I've done wrong. His code works and I cant see a typo in my code anywhere. Been on this on and off for over a day now and because I'm new to rust Im having a hard time figuring out how to solve. Can someone tell me whats wrong here and where I can find info about why its wrong? Maybe in the rustup docs? I would really appreciate any help. Thank you.

The pattern should just have the name title with no type annotation:

Book::Other(title) => title.into(),

The code in the video looks different because it has inlay hints displayed by the creator’s IDE or text editor. These are not part of the code; they are like extra comments that appear automatically when using rust-analyzer or similar plugins/features.

4 Likes

Wow... I had a feeling it would be something simple. Thank you a bunch. :slight_smile:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.