Rust 功能行为取决于是否实现特征

Rust 功能行为取决于是否实现特征,rust,Rust,在这种情况下: trait Basic { // Some functions } trait Derived : Basic { // some other functions } fn do_someting<T : Basic>(target: &mut T) { // do stuff } trait-Basic{ //一些功能 } 性状来源:基础{ //其他一些功能 } fn do_someting(目标:&mut T T){ //做事

在这种情况下:

trait Basic {
    // Some functions
}

trait Derived : Basic {
    // some other functions
}

fn do_someting<T : Basic>(target: &mut T) {
    // do stuff
}
trait-Basic{
//一些功能
}
性状来源:基础{
//其他一些功能
}
fn do_someting(目标:&mut T T){
//做事
}
根据
target
是否实现了trait
Derived
(编译时已知的信息),是否可能让
do\u something
函数的行为有所不同


我想在C++中使用模板特化可以实现类似的东西,但是我看不到一个锈等价物。

AFAK这是目前不可能的。将
fn do\u something
与现有函数一起定义会导致编译错误:

error: duplicate definition of value `do_someting`
因为这两个版本都可以用于Basic

有一些建议允许显式边界,如
,这将允许您解决此冲突

但我个人希望在1.0版本发布后的某一天,impl重载/专门化将成为可能