“where”关键字的语法和语义是什么?

What is the syntax and semantics of the `where` keyword?

不幸的是,Rust 关于 where 的文档非常缺乏。该关键字只出现在参考文献中一两个不相关的例子中。

  1. where在以下代码中有什么语义差异?有什么区别吗?首选哪种形式?

    fn double_a<T>(a:T) -> T where T:std::num::Int {
        a+a
    }
    
    fn double_b<T: std::num::Int>(a:T) -> T {
        a+a
    }
    
  2. 在 CharEq 特性的实现中,似乎 where 被用作某种 "selector" 来为任何匹配某种闭包类型的东西实现特性。我说的对吗?

有什么方法可以获得更好、更完整的 where 图片? (用法和语法的完整规范)

在你的例子中,这两个代码是严格等价的。

引入了 where 子句以允许更具表现力的边界检查,例如:

fn foo<T>(a: T) where Bar<T>: MyTrait { /* ... */ }

仅使用旧语法是不可能的。

使用 where 而不是原始语法通常是为了提高可读性而首选,即使仍然可以使用旧语法也是如此。

你可以想象像这样的结构

fn foo<A, B, C>(a: A, b: B, c: C)
    where A: SomeTrait + OtherTrait,
          B: ThirdTrait<A>+ OtherTrait,
          C: LastTrait<A, B>
{
    /* stuff here */
}

这种方式可读性更高,即使仍然可以使用旧语法来表达。

关于 CharEq 特征的问题,代码是:

impl<F> CharEq for F where F: FnMut(char) -> bool {
    #[inline]
    fn matches(&mut self, c: char) -> bool { (*self)(c) }

    #[inline]
    fn only_ascii(&self) -> bool { false }
}

它的字面意思是:为所有已经实现了特征FnMut(char) -> bool的类型F实现特征CharEq(即闭包或函数采用char 并返回 bool).

更多细节,你可以查看引入where条款的RFC:https://github.com/rust-lang/rfcs/pull/135