[英]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
类型以任何方式相关。
编译器看到V
和V: VisibleElement
,然后它会抛弃它们,因为它们对以下内容没有影响:
impl trait
) self type
( self type
) predicates
或边界)的任何约束。 例如,如果谓词包含V
和S
之间的关系,那么这将是有意义的,因为它将添加有关在此定义哪些实现的信息。 例如,这可能是这样的:
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.