rustrust-dieselrustdoc

What does the `tags` variable come from in the diesel documentation?


I'm looking at the official Diesel documentation on this page: https://docs.diesel.rs/diesel/expression_methods/trait.PgArrayExpressionMethods.html#example. The example uses a variable tags, but I cannot see where it is defined. How should I understand this code snippet?

let cool_posts = posts.select(id)
    .filter(tags.contains(vec!["cool"]))
    .load::<i32>(&conn)?;
assert_eq!(vec![1], cool_posts);

let amazing_posts = posts.select(id)
    .filter(tags.contains(vec!["cool", "amazing"]))
    .load::<i32>(&conn)?;

Where does the tags variable come from?


Solution

  • Examples in Rust docs can hide parts of the code using #. This is usually done to reduce noise of repeated boilerplate, like main functions and imports.

    If you view the source for that trait, you can see the full code contains:

    /// # table! {
    /// #     posts {
    /// #         id -> Integer,
    /// #         tags -> Array<VarChar>,
    /// #     }
    /// # }
    

    The lines starting with # are not rendered in the documentation, but are still compiled when you run the documentation tests.

    Presumably, in the context of Diesel examples, this is common enough or perhaps considered "obvious" enough that the authors thought the example was clearer without it. This is probably a matter of opinion, which not everybody will agree with.