首页 > 解决方案 > X 类型的值的大小在编译时是未知的

问题描述

我正在尝试 Rust 并努力解决许多基本问题,例如错误本身。我有以下代码(使用 Termion lib,但我确信这无关紧要)

extern crate termion;

use termion::color;

static bg_white: color::Bg<color::Color> = color::Bg(color::Rgb(255, 255, 255));
static bg_reset: color::Bg<color::Color> = color::Bg(color::Reset);

fn main() {
    println!("Hello, world!");
}

当我使用 编译它时cargo run,我收到以下错误:

error[E0277]: the size for values of type `(dyn termion::color::Color + 'static)` cannot be known at compilation time
 --> src/main.rs:5:1
  |
5 | static bg_white: color::Bg<color::Color> = color::Bg(color::Rgb(255, 255, 255));
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `std::marker::Sized` is not implemented for `(dyn termion::color::Color + 'static)`
  = note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch19-04-advanced-types.html#dynamically-sized-types-and-sized>
  = note: required by `termion::color::Bg`

error[E0277]: the size for values of type `(dyn termion::color::Color + 'static)` cannot be known at compilation time
 --> src/main.rs:6:1
  |
6 | static bg_reset: color::Bg<color::Color> = color::Bg(color::Reset);
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `std::marker::Sized` is not implemented for `(dyn termion::color::Color + 'static)`
  = note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch19-04-advanced-types.html#dynamically-sized-types-and-sized>
  = note: required by `termion::color::Bg`

这些错误是如何产生的,我能做些什么来解决它们?

标签: rust

解决方案


推荐阅读