使用通用类型不匹配的类型

时间:2018-09-12 14:10:43

标签: generics rust

我正在尝试通过对游戏进行编程来学习Rust,但是在使用泛型时遇到错误:

EclipseKeys.withSource := true
EclipseKeys.withJavadoc := true

错误:

use termion::{cursor, style, clear};
use termion::raw::IntoRawMode;
use std::io::{Write, stdout};

struct Game<W> {
    stdout: W,
}

impl<W: Write> Game<W> {
    fn init() -> Game<W> {
        let stdout = stdout();
        let mut stdout = stdout.lock().into_raw_mode().unwrap();

        write!(stdout, "{}{}", clear::All, cursor::Goto(1, 1)).unwrap();
        stdout.flush().unwrap();

        Game {
            stdout,
        }
    }
}

我认为当我使用泛型类型时,该类型无关紧要,但正如编译器所说的那样...

如果我将error[E0308]: mismatched types --> src/main.rs:109:13 | 109 | stdout, | ^^^^^^ expected type parameter, found struct `termion::raw::RawTerminal` | = note: expected type `W` found type `termion::raw::RawTerminal<std::io::StdoutLock<'_>>` 方法更改为不返回init(),则代码可以正常工作,因此错误必须来自Game,但如果没有通用代码,此代码也会失败... < / p>

我在做什么错?

0 个答案:

没有答案