Interface rust语言中接口实现的递归调用
如何从接口的实现中递归调用我正在实现的接口 我正在做以下事情:Interface rust语言中接口实现的递归调用,interface,recursion,implementation,rust,Interface,Recursion,Implementation,Rust,如何从接口的实现中递归调用我正在实现的接口 我正在做以下事情: import io::*; import to_str::*; enum some_enum { foo(~[some_enum]), bar } impl to_str for some_enum { fn to_str() -> str { alt self { bar { "bar" } foo(nest) { "foo" + nest.to_str() } // this
import io::*;
import to_str::*;
enum some_enum {
foo(~[some_enum]),
bar
}
impl to_str for some_enum {
fn to_str() -> str {
alt self {
bar { "bar" }
foo(nest) { "foo" + nest.to_str() } // this line
}
}
}
fn main() {
println(foo(~[bar,bar,bar]).to_str());
}
并且它在编译时失败
user@note ~/soft/mine/rust $ rustc example.rs && ./example
example.rs:13:32: 13:43 error: failed to find an implementation of interface core::to_str::to_str for some_enum
example.rs:13 foo(nest) { "foo" + nest.to_str() }
^~~~~~~~~~~
当然,我可以做类似FP的事情:
foo(nest) { "foo" + nest.map(|x| { x.to_str() }).to_str() }
但为什么前一种情况无效?似乎可以用
impl of
而不是impl
来解决
impl
没有of
就像没有接口的实现一样,不涉及实际的接口
(确认)