以下是一些示例代码:
c $ c>使用std :: marker :: PhantomData; 酒吧特质Foo { fn foo(& self); } 酒吧特色酒吧< A:Foo> { fn bar(& self,a:A); } pub结构体测试< A,B> 其中A:Foo, B:Bar< A> {_phantom_r:PhantomData< A> ;, bars:Vec< B>,} impl< A,B>测试< A,B> 其中A:Foo, B:Bar< A> { pub fn new() - >测试< A,B> { Test { _phantom_r:PhantomData, bars:Vec :: new(),} } pub fn add_bar( & mut self,b:B){ self.bars.push(b); } } fn main(){ let t = Test :: new();( Playground )
错误是:
< anon>:32:13:36:22错误:无法推断出关于`_`的足够的类型信息;类型注释或通用参数绑定[E0282] :32 let t = Test :: new();我对Rust在推断特质类型时遇到困难以及我如何指定它想要什么。也就是说,我不确定这是否正确,因为那时我碰到了 Sized 问题:
let t = Test :: new()as Test< Foo,Bar< Foo>> ;;错误:
< anon>:36:28:36:46错误:特征`core :: marker :: Sized`没有针对`Foo`类型实现[E0277] < anon> :36 let t = Test :: new()as Test< Foo,Bar< Foo>>;我有两个主要问题:
解释您的声明:
pub trait Foo {}有一个特性 Foo
pub trait Bar< A:Foo> {}如果您给我一个 A $ c $我可以给你一个特质 Bar< A> 。
pub struct Test< A,B> 其中A:Foo, B:Bar< A> {}如果您给我类型 A ,它实现了 Foo 和 B ,它实现了 Bar< A> ,我给你一个测试< A,B> 的类型。
let t = Test :: new();code> Test 这是问题 - Test 不是一个类型,它是一个用于给出另外两个类型的模板类型(有一些限制)在上面的例子中,你没有提供任何类型,只是缩小了类型的类型。
要实际使用 Test ,您需要提供以下类型:
struct MyA {} $ impl Foo for MyA { fn foo(& self){println!(MyA :: foo);} } struct MyB {} $ impl Bar MyA> MyB { fn bar(& self,a:MyA){println!(MyB :: bar);} } fn main(){ let test = Test ::< MyA,MyB> ::新();( Playground )
Apologies for the generic title.
Here is some example code:
use std::marker::PhantomData; pub trait Foo { fn foo(&self); } pub trait Bar<A: Foo> { fn bar(&self, a: A); } pub struct Test<A, B> where A: Foo, B: Bar<A> { _phantom_r: PhantomData<A>, bars: Vec<B>, } impl<A, B> Test<A, B> where A: Foo, B: Bar<A> { pub fn new() -> Test<A, B> { Test { _phantom_r: PhantomData, bars: Vec::new(), } } pub fn add_bar(&mut self, b: B) { self.bars.push(b); } } fn main() { let t = Test::new(); }(Playground)
The error is:
<anon>:32:13: 36:22 error: unable to infer enough type information about `_`; type annotations or generic parameter binding required [E0282] <anon>:32 let t = Test::new();I'm quite confused on what Rust is having trouble inferring the trait types, and how I can specify what it wants. That is, I'm not sure if this is right, because then I run into Sized issues:
let t = Test::new() as Test<Foo,Bar<Foo>>;error:
<anon>:36:28: 36:46 error: the trait `core::marker::Sized` is not implemented for the type `Foo` [E0277] <anon>:36 let t = Test::new() as Test<Foo,Bar<Foo>>;I have two main questions:
解决方案
The short answer is that you haven't told it what type to use.
Paraphrasing your declarations:
pub trait Foo {}"There is a trait Foo"
pub trait Bar<A: Foo> {}"If you give me a type A which implements Foo, I can give you a trait Bar<A>."
pub struct Test<A, B> where A: Foo, B: Bar<A> {}"If you give me types A, which implements Foo, and B, which implements Bar<A>, I'll give you a type Test<A,B>.
let t = Test::new();"Make me a Test". This is the problem - Test isn't a type, it's a template for making a type given two other types (with some restrictions). In the example above you haven't provided any such types, just narrowed down what such types might be like.
To actually use Test, you need to provide the types:
struct MyA {} impl Foo for MyA { fn foo(&self) { println!("MyA::foo"); } } struct MyB {} impl Bar<MyA> for MyB { fn bar(&self, a: MyA) { println!("MyB::bar"); } } fn main() { let test = Test::<MyA, MyB>::new(); }(Playground)
更多推荐
错误:无法推断出有关`
发布评论