LongHike
LongHike

Reputation: 4460

Cannot assign vector value to field in struct: expected one of 8 possible tokens

I have this struct:

struct PaginatedTopics {
    docs: Vec<Topic>,
    pages: u32,
}

and I want to assign a vector of Topic type to it like this:

let topics = conn.query_map(
    r"SELECT id, title",
    |(id, title)| {
        Topic { id, title, ..Default::default() }
    });

let mut paginated_results = {
    docs: topics,
    pages: 0,
};

This is not working; I keep getting the error:

error: expected one of `!`, `(`, `.`, `::`, `;`, `<`, `?`, or `}`, found `,`
 --> src/lib.rs:9:17
  |
9 |     docs: topics,
  |         -       ^ expected one of 8 possible tokens
  |         |
  |         tried to parse a type due to this

I don't understand that error. It's a simple assignment.

Upvotes: 0

Views: 582

Answers (1)

Aplet123
Aplet123

Reputation: 35512

Rust doesn't have anonymous structs, you have to provide their name:

let mut paginated_results = PaginatedTopics {
    docs: topics,
    pages: u32,
};

See The Rust Programming Language for more information.

Upvotes: 4

Related Questions