#![feature(specialization)]
struct Diver<T> {
inner:T
}
trait Swimmer {
fn swim(&self){
println!("swimming")
}
}
impl<T> Swimmer for Diver<T>{}
impl Swimmer for Diver<&'static str> {
fn swim(&self){
println!("drowning, help!")
}
}
fn main(){
let x = Diver::<&'static str> { inner: "Bob" };
x.swim(); //drowning, help!
let y = Diver::<String> { inner: String::from("Alice") };
y.swim(); // swimming
}
错误信息:
Compiling playground v0.0.1 (/playground)
error[E0520]: `swim` specializes an item from a parent `impl`, but that item is not marked `default`
--> src/main.rs:12:5
|
10 | impl<T> Swimmer for Diver<T>{}
| ------------------------------ parent `impl` is here
11 | impl Swimmer for Diver<&'static str> {
12 | / fn swim(&self){
13 | | println!("drowning, help!")
14 | | }
| |_____^ cannot specialize default item `swim`
|
= note: to specialize, `swim` in the parent `impl` must be marked `default`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0520`.
error: could not compile `playground`.
To learn more, run the command again with --verbose.
页码与行数:
代码清单 3-78:(随书源码)
错误信息:
修改为如下:
在线测试