错误:无法推断出有关`

编程入门 行业动态 更新时间:2024-10-23 21:36:57
本文介绍了错误:无法推断出有关`_`的足够的类型信息;类型注释或通用参数绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

<$ <$ p $ b

以下是一些示例代码:

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>>;

我有两个主要问题:

  • 为什么Rust无法推断 Test< A,B<>> ?
  • 使这段代码有效的解决方案是什么? 解决方案

    解释您的声明:

    pub trait Foo {}

    有一个特性 Foo

    pub trait Bar< A:Foo> {}

    如果您给我一个 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:

  • Why is Rust unable to infer the trait types of Test<A,B<A>>?
  • What is the solution to make this code work?
  • 解决方案

    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)

    更多推荐

    错误:无法推断出有关`

    本文发布于:2023-10-25 21:56:18,感谢您对本站的认可!
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:推断   错误

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!