简体   繁体   English

不匹配的类型预期特征 object `dyn Trait` 找到结构 `Struct`

[英]mismatched types expected trait object `dyn Trait` found struct `Struct`

I have those structs and trait:我有那些结构和特征:

use std::io;

pub struct Human {}

impl Human {
    pub fn new () -> Self {
        Self {}
    }
}
pub struct Robot {
    previous_guess: u32
}

impl Robot {
    pub fn new () -> Self {
        Self {
            previous_guess: 0
        }
    }
}

pub trait Guesser {
    fn guess(&mut self) -> String;
}

impl Guesser for Human {
    fn guess(&mut self) -> String {
        let mut curr_guess = String::new();
        io::stdin()
            .read_line(&mut curr_guess)
            .expect("Failed to read line");
        curr_guess
    }
}

impl Guesser for Robot {
    fn guess(&mut self) -> String {
        self.previous_guess = self.previous_guess + 1;
        self.previous_guess.to_string()
    }
}

and I want to store one of them according to the user input but I get:我想根据用户输入存储其中之一,但我得到:

fn main() {
    let player_type = get_player_type().unwrap();

// ERROR - mismatched types expected trait object `dyn Guesser` found struct `Human`
    let player: dyn Guesser = match player_type {
        PlayerType::Human => Human::new(),
        PlayerType::Robot => Robot::new()
    };
}


fn get_player_type() -> Result<PlayerType, String> {
    let mut is_human = String::new();
    println!("Who would you like to see playing ? (me / robot):");
    io::stdin()
        .read_line(&mut is_human)
        .expect("Failed to read line");

    match is_human.trim().to_lowercase().as_ref() {
        "me" => { Ok(PlayerType::Human) },
        "robot" => { Ok(PlayerType::Robot) },
        _ => { Err("Please type 'me' or 'robot'".to_string()) }
    }
}

and I don't understand how I get this error for the Human but not for the Robot struct neither how to solve it while both implement the Guesser trait... I tried using Box<dyn Guesser> but it doesn't work either.而且我不明白我是如何为 Human 而不是为 Robot 结构得到这个错误的,也不知道如何在两者都实现 Guesser 特征的同时解决它......我尝试使用Box<dyn Guesser>但它也不起作用。

Actually I just tried something else, if you add Box<dyn Guesser> you apparently need to instanciate it differently:实际上我只是尝试了别的东西,如果你添加Box<dyn Guesser>你显然需要以不同的方式实例化它:

    let player: Box<dyn Guesser> = match player_type {
        PlayerType::Human => Box::new(Human::new()),
        PlayerType::Robot => Box::new(Robot::new())
    };

This worked for me这对我有用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为字段创建具有通用特征的结构。 预期结构 <trait> 找到结构 <type that implements said trait> - Creating a struct with a generic trait for field. Expected struct<trait> found struct<type that implements said trait> 预期特征 object `dyn Responsability`,找到类型参数 `T` - expected trait object `dyn Responsability`, found type parameter `T` Rust 预期类型不匹配 (),找到结构`Enumerate - Rust mismatched types expected (), found struct `Enumerate 实现特征时“预期的类型参数,找到的结构” - “expected type parameter, found struct” when implementing trait 结构特征错误:arguments 类型的数量错误:预期为 1,发现为 0 - Struct trait error: Wrong number of type arguments: Expected 1, found 0 将泛型结构视为特征 object - Treat generic struct as trait object 向下转换 Rc <refcell<box<struct> &gt;&gt; 到 Rc <refcell<box<dyn trait> &gt;&gt; </refcell<box<dyn></refcell<box<struct> - Downcasting a Rc<RefCell<Box<struct>>> to Rc<RefCell<Box<dyn trait>>> 结构不会强制在 function 返回中实现 dyn Trait 类型 - Struct won't coerce to implemented dyn Trait type in function return 返回的结构类型不匹配(预期 <K, V> ,找到&lt;&K,&V&gt;) - Mismatched types for returned struct (expected <K, V>, found <&K, &V>) Rust 显示预期特征 object `dyn Future`,在传递 function 作为参数时发现不透明类型 - Rust showing expected trait object `dyn Future`, found opaque type when passing function as a param
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM