【问题标题】:What does the `tags` variable come from in the diesel documentation?柴油文档中的 `tags` 变量来自什么?
【发布时间】:2021-11-07 12:39:26
【问题描述】:

我正在查看此页面上的官方 Diesel 文档:https://docs.diesel.rs/diesel/expression_methods/trait.PgArrayExpressionMethods.html#example。该示例使用变量tags,但我看不到它的定义位置。这段代码sn-p应该怎么理解?

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)?;

tags 变量从何而来?

【问题讨论】:

  • 我终于发现标签是 vec。是否可以在柴油查询中使用 in select ? @彼得霍尔

标签: rust rust-diesel rustdoc


【解决方案1】:

Rust 文档中的示例可以使用 # 隐藏部分代码。这样做通常是为了减少重复样板的噪音,例如 main 函数和导入。

如果您查看 the source 的该特征,您可以看到完整的代码包含:

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

以# 开头的行不会在文档中呈现,但在您运行文档测试时仍会编译。

据推测,在 Diesel 示例的上下文中,这很常见,或者可能被认为足够“明显”,以至于作者认为没有它的示例会更清晰。这可能是一个见仁见智的问题,并不是每个人都会同意。

【讨论】:

  • 我检查了源代码并阅读了它,发现标签是一个数组,是否可以使用柴油进行选择?我现在想使用柴油进行查询,但没有找到方法。 @彼得霍尔
猜你喜欢
  • 2021-05-10
  • 2022-12-10
  • 2022-12-10
  • 2021-07-06
  • 2021-03-17
  • 2022-01-20
  • 2021-07-25
  • 2020-04-14
  • 2022-12-10
相关资源
最近更新 更多