繁体   English   中英

Rust中的谓词是什么?

[英]What is a predicate in Rust?

当我尝试编译此代码时:

impl<S, V> Storage for Database<S>
where
    S: StoredElement,
    V: VisibleElement,

编译器抱怨

error[E0207]: the type parameter `V` is not constrained by the impl trait, self type, or predicates
  --> src/main.rs:77:9
   |
77 | impl<S, V> Storage for Database<S>
   |         ^ unconstrained type parameter

我假设V: VisibleElement是一个谓词,但显然编译器不同意。

那么,在Rust中,谓词究竟是什么?

V: VisibleElement 此上下文中的谓词。 问题在于谓词不足以使V类型以任何方式相关。

编译器看到VV: VisibleElement ,然后它会抛弃它们,因为它们对以下内容没有影响:

  • 你正在实施哪种特质( impl trait
  • 或您实施特征的self typeself type
  • 或其中任何一个( predicates或边界)的任何约束。

例如,如果谓词包含VS之间的关系,那么这将是有意义的,因为它将添加有关在此定义哪些实现的信息。 例如,这可能是这样的:

impl<S, V> Storage for Database<S>
where
    S: StoredElement<ChildType = V>,
    V: VisibleElement,

我在这里编写了类型,因为我不知道实际类型的来源。 这将是对V的有意义的用法,因为它不仅StoredElement S约束为StoredElement而且将StoredElement约束为其关联的ChildType实现VisibleElement ChildType 这将仅定义满足该条件(谓词)的Database Storage的实现。

编译器抱怨,因为你添加了一个没有任何影响的参数,这很可能是你的错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM