具有大小supertrait的特征仍然存在错误" std :: marker :: Sized不满意"

时间:2018-02-28 20:56:03

标签: rust traits

我有以下代码:

use std::collections::HashMap;

trait T: Sized {}

struct A;

impl T for A {}

fn main() {
    let h: HashMap<String, T>;
}

但是编译器抱怨道:

error[E0277]: the trait bound `T: std::marker::Sized` is not satisfied
  --> src\main.rs:10:12
   |
10 |     let h: HashMap<String, T>;
   |            ^^^^^^^^^^^^^^^^^^ `T` does not have a constant size known at compile-time
   |
   = help: the trait `std::marker::Sized` is not implemented for `T`
   = note: required by `std::collections::HashMap`

error[E0038]: the trait `T` cannot be made into an object
  --> src\main.rs:10:12
   |
10 |     let h: HashMap<String, T>;
   |            ^^^^^^^^^^^^^^^^^^ the trait `T` cannot be made into an object
   |
   = note: the trait cannot require that `Self : Sized`

我不理解错误消息,因为我已将我的特征T标记为Sized。我错过了什么吗?

1 个答案:

答案 0 :(得分:5)

  

因为我已将我的特质from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait browser = webdriver.Chrome("/Users/Desktop/projects/chromedriver") browser.get('https://www.google.com') elem = browser.find_element_by_id('lst-ib') WebDriverWait(elem, 5) elem.send_keys('stackoverflow' + Keys.RETURN) # How to force selenium for 5 seconds before pressing enter 标记为bazel fetch @boost_test//...

不,你没有。您已经说过,实现T 的任何类型都必须是Sized trait 本身仍未确定。你需要一个特质对象(例如T)或某种泛型(在这种情况下你不能做)。