我想用泛型计算一个阶乘,但是我得到了一个错误main.
我的完整代码:
pub trait Body {
fn new() -> Self;
fn fact(&self, x: usize) -> usize {
match x {
1 => 1,
_ => x * self.fact(x - 1),
}
}
}
#[derive(Clone, Debug)]
pub struct RecursiveCall<T: Body> {
level: usize,
indicator: String,
n_repeat: usize,
body: T,
}
impl<T> RecursiveCall<T>
where T: Body
{
fn new(n_repeat: usize) -> RecursiveCall<T> {
RecursiveCall {
level: 0,
indicator: "- ".to_string(),
n_repeat: n_repeat,
body: <T as Body>::new(),
}
}
fn pre_trace(&self, fname: &str, arg: &usize) {
let args: String = arg.to_string();
println!("{}",
(vec![self.indicator.as_str(); self.level]).join("") +
self.level.to_string().as_str() + ":" + fname + "(" +
args.as_str() + ")");
}
fn post_trace(&self, fname: &str, arg: &usize, ret: &usize) {
println!("{}",
(vec![self.indicator.as_str(); self.level]).join("") +
self.level.to_string().as_str() + ":" + fname + "=" +
ret.to_string().as_str());
}
fn print_trace(&mut self) {
&self.pre_trace("fact", &self.n_repeat);
self.level += 1;
let ret = &self.body.fact(self.n_repeat);
self.level -= 1;
&self.post_trace("fact", &self.n_repeat, ret);
println!("Difference={}", &ret.to_string().as_str());
}
}
type B = Body;
fn main() {
let t = RecursiveCall::<B>::new();
}
Run Code Online (Sandbox Code Playgroud)
此错误发生在main():
error: no associated item named `new` found for type `RecursiveCall<Body + 'static>` in the current scope
--> src/main.rs:61:13
|
61 | let t = RecursiveCall::<B>::new();
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= note: the method `new` exists but the following trait bounds were not satisfied: `Body : std::marker::Sized`, `Body : Body`
= help: items from traits can only be used if the trait is implemented and in scope; the following traits define an item `new`, perhaps you need to implement one of them:
= help: candidate #1: `Body`
= help: candidate #2: `std::sys_common::thread_info::NewThread`
= help: candidate #3: `std::iter::ZipImpl`
error[E0277]: the trait bound `Body + 'static: std::marker::Sized` is not satisfied
--> src/main.rs:61:13
|
61 | let t = RecursiveCall::<B>::new();
| ^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::marker::Sized` is not implemented for `Body + 'static`
|
= note: `Body + 'static` does not have a constant size known at compile-time
= note: required by `RecursiveCall`
error[E0277]: the trait bound `Body + 'static: Body` is not satisfied
--> src/main.rs:61:13
|
61 | let t = RecursiveCall::<B>::new();
| ^^^^^^^^^^^^^^^^^^^^^^^ the trait `Body` is not implemented for `Body + 'static`
|
= note: required by `RecursiveCall`
error[E0038]: the trait `Body` cannot be made into an object
--> src/main.rs:61:13
|
61 | let t = RecursiveCall::<B>::new();
| ^^^^^^^^^^^^^^^^^^^^^^^ the trait `Body` cannot be made into an object
|
= note: method `new` has no receiver
Run Code Online (Sandbox Code Playgroud)
答案的关键在于编译器的这个注释:
注意:该方法
new存在,但以下特征边界并不满意:Body : std::marker::Sized,Body : Body
您已将其定义B为类型别名Body,因此名称B和Body两者在程序中的含义相同(至少在此模块中).
定义特征时,编译器还会定义具有相同名称的类型.但是,该类型无法直接实例化(与C++/C#/ Java /等中的类不同).然而,这正是你想要做的!
特征限制Body : std::marker::Sized不满足,因为Body作为与同名特征相对应的编译器定义的类型是未大小的类型.未施胶类型只能在指针和引用(例如可使用&Body,Box<Body>等等).
Body : Body由于您的特性不是对象安全的,因此不满足特征限制.它不是对象安全的,因为该方法new没有self参数(这是编译器在最后一个注释中的含义:) method `new` has no receiver.
通常,您将定义结构或枚举并实现该类型的特征,然后在实例化时使用该类型RecursiveCall.尝试替换type B = Body;以下内容:
struct B;
impl Body for B {
fn new() -> B {
B
}
}
Run Code Online (Sandbox Code Playgroud)