这是Rust的assert_eq!宏实现.为简洁起见,我只复制了第一个分支:
macro_rules! assert_eq {
($left:expr, $right:expr) => ({
match (&$left, &$right) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
panic!(r#"assertion failed: `(left == right)`
left: `{:?}`,
right: `{:?}`"#, left_val, right_val)
}
}
}
});
}
Run Code Online (Sandbox Code Playgroud)
这里的目的是match什么?为什么不检查不平等?
以下代码导致错误(Playground)
#![feature(specialization)]
trait Foo {
type Assoc;
fn foo(&self) -> &Self::Assoc;
}
default impl<T> Foo for T {
type Assoc = T;
fn foo(&self) -> &Self::Assoc {
self
}
}
Run Code Online (Sandbox Code Playgroud)
错误:
error[E0308]: mismatched types
--> src/main.rs:20:9
|
20 | self
| ^^^^ expected associated type, found type parameter
|
= note: expected type `&<T as Foo>::Assoc`
found type `&T`
Run Code Online (Sandbox Code Playgroud)
这是奇怪<T as Foo>::Assoc 的 T,所以它应该工作.甚至更奇怪:当我default从impl中删除关键字时,它可以工作(但当然,在我的真实代码中,我需要将impl标记为default).
在特征定义(Playground)中提供默认值时会发生同样的错误:
#![feature(specialization)]
#![feature(associated_type_defaults)]
trait Foo { …Run Code Online (Sandbox Code Playgroud)