gpt4 book ai didi

rust - 我如何对待泛型?

转载 作者:行者123 更新时间:2023-11-29 08:03:54 24 4
gpt4 key购买 nike

我想使用泛型计算阶乘,但在 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();
}

此错误发生在 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

最佳答案

答案的关键在编译器的注释中:

note: the method new exists but the following trait bounds were not satisfied: Body : std::marker::Sized, Body : Body

您已经定义了 B作为 Body 的类型别名, 所以名字 BBody在您的程序中(至少在本模块中)两者意思相同。

当你定义一个特征时,编译器也会定义一个同名的类型。但是,该类型不能直接实例化(与 C++/C#/Java 等中的类不同)。然而,这正是您想要做的!

特质界限 Body : std::marker::Sized不满意因为Body ,是编译器定义的类型,对应于同名特征,是一个未确定大小的类型。未确定大小的类型只能用于指针和引用(例如 &BodyBox<Body> 等)。

特质界限 Body : Body不满意,因为你的特质不是object-safe .它不是对象安全的,因为方法 new没有 self参数(这是编译器在最后一个注释中的意思:method `new` has no receiver)。

通常,您会定义一个结构或枚举并实现该类型的特征,然后在实例化时使用该类型 RecursiveCall .尝试替换 type B = Body;具有以下内容:

struct B;

impl Body for B {
fn new() -> B {
B
}
}

关于rust - 我如何对待泛型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42380121/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com