【发布时间】:2015-06-07 21:21:27
【问题描述】:
根据the docs,如果我尝试调用由两个不同特征提供的方法,Rust 应该会抱怨:
trait Foo {
fn f(&self);
}
trait Bar {
fn f(&self);
}
struct Baz;
impl Foo for Baz {
fn f(&self) { println!("Baz’s impl of Foo"); }
}
impl Bar for Baz {
fn f(&self) { println!("Baz’s impl of Bar"); }
}
fn main(){
let b = Baz;
b.f();
}
运行此程序会导致预期的error: multiple applicable methods in scope 错误。
但是我没有得到任何错误:
extern crate mio;
use mio::buf::RingBuf;
use mio::buf::Buf;
use std::io::Read;
fn main() {
let buf = RingBuf::new(10);
let bytes = buf.bytes();
println!("{:?}", bytes);
}
mio::buf::RingBuf 实现了Buf 和Read。这两个特征都提供了一个bytes 方法。
我希望 Rust 会抱怨与上述相同的错误。相反,它默默地选择了“错误”的实现,后来println 抱怨错误的类型。
知道为什么我在这里没有收到错误吗?
如果我删除 use std::io::Read; 一切正常。但是由于范围内的特性突然使用了 Read 的实现,并且 bytes 具有“错误”类型。
(我使用的是 Rust 1.0.0)
【问题讨论】:
-
请在rust's issue tracker举报此问题
-
我已经这样做了:github.com/rust-lang/rust/issues/26080 - 不确定它是否是完美的地方。
-
谢谢,我想就是这个地方