根据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的实现,字节具有“错误”类型。
(我正在使用Rust 1.0.0)
答案 0 :(得分:7)
@bluss发现了问题:
@echo off
md axis > Nul <&2
for %%a in ("Axis_Master Group_*_*.*") do (
call :refolder "%%a"
)
exit /b
:refolder
set fileName=%~n1
set fileName=%fileName:Axis_Master Group_=%
for /f "delims=_ tokens=1*" %%a in ("%fileName%") do (
md axis\%%a > Nul <&2
move /y %1 axis\%%a\%%b%~x1 > Nul <&2
)
exit /b
如果两种类型使用稍微不同的struct Type;
trait A {
fn foo(&self) -> bool { false }
}
trait B : Sized {
fn foo(self) -> bool { true }
}
impl A for Type { }
impl B for Type { }
fn main() {
println!("{}", Type.foo()); // This will call B::foo -- it will prefer `self`.
}
类型,则Rust将它们视为不同类型,并且调用该方法只是更喜欢其中一种。
这可能是Rust的一个错误。有关详细信息,请查看相应的Rust issue。