私はそれはBox<T>
がT
でこれを課していないとき、それはRng
にSized
を必要とする理由私は理解していないTが特性オブジェクトである場合、どのようにボックス<T>を作成しますか?
error[E0277]: the trait bound `rand::Rng: std::marker::Sized` is not satisfied
--> src/main.rs:16:21
|
16 | let mut r = Box::new(*rng);
| ^^^^^^^^ `rand::Rng` does not have a constant size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `rand::Rng`
= note: required by `<std::boxed::Box<T>>::new`
と文句を言い、次のコード
extern crate rand;
use rand::Rng;
pub struct Randomizer {
rand: Box<Rng>,
}
impl Randomizer {
fn new() -> Self {
let mut r = Box::new(rand::thread_rng()); // works
let mut cr = Randomizer { rand: r };
cr
}
fn with_rng(rng: &Rng) -> Self {
let mut r = Box::new(*rng); // doesn't work
let mut cr = Randomizer { rand: r };
cr
}
}
fn main() {}
を持っています。
これは実際には何の関係もありません。後でこれをメンバーに保存して、タイトルがかなり不正確になり、さらに検索者をあまり助けてくれることはありません。もし誰かがよりよいアイデアを考えていたら... –
[trait objects](https://doc.rust-lang.org/book/trait-objects.html)について知っていますか? @MatthieuM。 – kennytm
。あなたはタイトルが改善する必要があることは間違いありません。 – Andreas