When I try to compile this line, opt.positionals = Positionals::try_from(opt.args).unwrap(); I'm getting an error about a trait not being implement. This trait should be implemented though generically as I have TryFrom<Vec<T>>
Here is the error,
error[E0277]: the trait bound `Positionals: TryFrom<Vec<String>>` is not satisfied
--> bin/seq.rs:83:20
|
83 | opt.positionals = Positionals::try_from(opt.args).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `TryFrom<Vec<String>>` is not implemented for `Positionals`
|
= help: the following implementations were found:
<Positionals as TryFrom<Vec<T>>>
And here is my implementation of the trait,
use std::convert::TryFrom;
impl<T: Debug + AsRef<String> + Into<String> + Clone> TryFrom<Vec<T>> for Positionals {
Why doesn't my implementation of TryFrom<Vec<T>> cover the concrete TryFrom<Vec<String>> and how can I remedy this problem?
String does not implement AsRef<String>. You could use AsRef<str> or Borrow<String> instead.
I wanted to install flamegraph for Rust. Following the instructions available in the README I run cargo install flamegraph. However, at some point it throws a bunch of errors:
Compiling env_logger v0.8.3
Compiling atty v0.2.14
Compiling num_cpus v1.13.0
Compiling signal-hook-registry v1.3.0
Compiling quick-xml v0.20.0
Compiling clap v2.33.3
error[E0277]: the trait bound `K: std::cmp::Eq` is not satisfied
--> /Users/ania/.cargo/registry/src/github.com-1ecc6299db9ec823/ahash-0.6.3/src/hash_map.rs:36:18
|
34 | impl<K, V> AHashMap<K, V, RandomState> {
| - help: consider restricting this bound: `K: std::cmp::Eq`
35 | pub fn new() -> Self {
36 | AHashMap(HashMap::with_hasher(RandomState::default()))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::cmp::Eq` is not implemented for `K`
|
= note: required by `std::collections::HashMap::<K, V, S>::with_hasher`
error[E0277]: the trait bound `K: std::hash::Hash` is not satisfied
--> /Users/ania/.cargo/registry/src/github.com-1ecc6299db9ec823/ahash-0.6.3/src/hash_map.rs:36:18
|
34 | impl<K, V> AHashMap<K, V, RandomState> {
| - help: consider restricting this bound: `K: std::hash::Hash`
35 | pub fn new() -> Self {
36 | AHashMap(HashMap::with_hasher(RandomState::default()))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::hash::Hash` is not implemented for `K`
|
= note: required by `std::collections::HashMap::<K, V, S>::with_hasher`
error[E0277]: the trait bound `K: std::cmp::Eq` is not satisfied
--> /Users/ania/.cargo/registry/src/github.com-1ecc6299db9ec823/ahash-0.6.3/src/hash_map.rs:40:18
|
34 | impl<K, V> AHashMap<K, V, RandomState> {
| - help: consider restricting this bound: `K: std::cmp::Eq`
...
40 | AHashMap(HashMap::with_capacity_and_hasher(capacity, RandomState::default()))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::cmp::Eq` is not implemented for `K`
|
= note: required by `std::collections::HashMap::<K, V, S>::with_capacity_and_hasher`
error[E0277]: the trait bound `K: std::hash::Hash` is not satisfied
--> /Users/ania/.cargo/registry/src/github.com-1ecc6299db9ec823/ahash-0.6.3/src/hash_map.rs:40:18
|
34 | impl<K, V> AHashMap<K, V, RandomState> {
| - help: consider restricting this bound: `K: std::hash::Hash`
...
40 | AHashMap(HashMap::with_capacity_and_hasher(capacity, RandomState::default()))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::hash::Hash` is not implemented for `K`
|
= note: required by `std::collections::HashMap::<K, V, S>::with_capacity_and_hasher`
error[E0277]: the trait bound `K: std::cmp::Eq` is not satisfied
--> /Users/ania/.cargo/registry/src/github.com-1ecc6299db9ec823/ahash-0.6.3/src/hash_map.rs:47:18
|
44 | impl<K, V, S> AHashMap<K, V, S> where S: BuildHasher {
| - help: consider further restricting type parameter `K`: `, K: std::cmp::Eq`
...
47 | AHashMap(HashMap::with_hasher(hash_builder))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::cmp::Eq` is not implemented for `K`
|
= note: required by `std::collections::HashMap::<K, V, S>::with_hasher`
error[E0277]: the trait bound `K: std::hash::Hash` is not satisfied
--> /Users/ania/.cargo/registry/src/github.com-1ecc6299db9ec823/ahash-0.6.3/src/hash_map.rs:47:18
|
44 | impl<K, V, S> AHashMap<K, V, S> where S: BuildHasher {
| - help: consider further restricting type parameter `K`: `, K: std::hash::Hash`
...
47 | AHashMap(HashMap::with_hasher(hash_builder))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::hash::Hash` is not implemented for `K`
|
= note: required by `std::collections::HashMap::<K, V, S>::with_hasher`
error[E0277]: the trait bound `K: std::cmp::Eq` is not satisfied
--> /Users/ania/.cargo/registry/src/github.com-1ecc6299db9ec823/ahash-0.6.3/src/hash_map.rs:51:18
|
44 | impl<K, V, S> AHashMap<K, V, S> where S: BuildHasher {
| - help: consider further restricting type parameter `K`: `, K: std::cmp::Eq`
...
51 | AHashMap(HashMap::with_capacity_and_hasher(capacity, hash_builder))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::cmp::Eq` is not implemented for `K`
|
= note: required by `std::collections::HashMap::<K, V, S>::with_capacity_and_hasher`
error[E0277]: the trait bound `K: std::hash::Hash` is not satisfied
--> /Users/ania/.cargo/registry/src/github.com-1ecc6299db9ec823/ahash-0.6.3/src/hash_map.rs:51:18
|
44 | impl<K, V, S> AHashMap<K, V, S> where S: BuildHasher {
| - help: consider further restricting type parameter `K`: `, K: std::hash::Hash`
...
51 | AHashMap(HashMap::with_capacity_and_hasher(capacity, hash_builder))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::hash::Hash` is not implemented for `K`
|
= note: required by `std::collections::HashMap::<K, V, S>::with_capacity_and_hasher`
error[E0599]: no method named `fmt` found for struct `std::collections::HashMap<K, V, S>` in the current scope
--> /Users/ania/.cargo/registry/src/github.com-1ecc6299db9ec823/ahash-0.6.3/src/hash_map.rs:258:16
|
258 | self.0.fmt(fmt)
| ^^^ method not found in `std::collections::HashMap<K, V, S>`
|
= note: the method `fmt` exists but the following trait bounds were not satisfied:
`std::collections::HashMap<K, V, S> : std::fmt::Debug`
error[E0277]: the trait bound `T: std::cmp::Eq` is not satisfied
--> /Users/ania/.cargo/registry/src/github.com-1ecc6299db9ec823/ahash-0.6.3/src/hash_set.rs:34:18
|
32 | impl<T> AHashSet<T, RandomState> {
| - help: consider restricting this bound: `T: std::cmp::Eq`
33 | pub fn new() -> Self {
34 | AHashSet(HashSet::with_hasher(RandomState::default()))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::cmp::Eq` is not implemented for `T`
|
= note: required by `std::collections::HashSet::<T, S>::with_hasher`
error[E0277]: the trait bound `T: std::hash::Hash` is not satisfied
--> /Users/ania/.cargo/registry/src/github.com-1ecc6299db9ec823/ahash-0.6.3/src/hash_set.rs:34:18
|
32 | impl<T> AHashSet<T, RandomState> {
| - help: consider restricting this bound: `T: std::hash::Hash`
33 | pub fn new() -> Self {
34 | AHashSet(HashSet::with_hasher(RandomState::default()))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::hash::Hash` is not implemented for `T`
|
= note: required by `std::collections::HashSet::<T, S>::with_hasher`
error[E0277]: the trait bound `T: std::cmp::Eq` is not satisfied
--> /Users/ania/.cargo/registry/src/github.com-1ecc6299db9ec823/ahash-0.6.3/src/hash_set.rs:38:18
|
32 | impl<T> AHashSet<T, RandomState> {
| - help: consider restricting this bound: `T: std::cmp::Eq`
...
38 | AHashSet(HashSet::with_capacity_and_hasher(capacity, RandomState::default()))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::cmp::Eq` is not implemented for `T`
|
= note: required by `std::collections::HashSet::<T, S>::with_capacity_and_hasher`
error[E0277]: the trait bound `T: std::hash::Hash` is not satisfied
--> /Users/ania/.cargo/registry/src/github.com-1ecc6299db9ec823/ahash-0.6.3/src/hash_set.rs:38:18
|
32 | impl<T> AHashSet<T, RandomState> {
| - help: consider restricting this bound: `T: std::hash::Hash`
...
38 | AHashSet(HashSet::with_capacity_and_hasher(capacity, RandomState::default()))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::hash::Hash` is not implemented for `T`
|
= note: required by `std::collections::HashSet::<T, S>::with_capacity_and_hasher`
error[E0277]: the trait bound `T: std::cmp::Eq` is not satisfied
--> /Users/ania/.cargo/registry/src/github.com-1ecc6299db9ec823/ahash-0.6.3/src/hash_set.rs:45:18
|
42 | impl<T, S> AHashSet<T, S> where S: BuildHasher {
| - help: consider further restricting type parameter `T`: `, T: std::cmp::Eq`
...
45 | AHashSet(HashSet::with_hasher(hash_builder))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::cmp::Eq` is not implemented for `T`
|
= note: required by `std::collections::HashSet::<T, S>::with_hasher`
error[E0277]: the trait bound `T: std::hash::Hash` is not satisfied
--> /Users/ania/.cargo/registry/src/github.com-1ecc6299db9ec823/ahash-0.6.3/src/hash_set.rs:45:18
|
42 | impl<T, S> AHashSet<T, S> where S: BuildHasher {
| - help: consider further restricting type parameter `T`: `, T: std::hash::Hash`
...
45 | AHashSet(HashSet::with_hasher(hash_builder))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::hash::Hash` is not implemented for `T`
|
= note: required by `std::collections::HashSet::<T, S>::with_hasher`
error[E0277]: the trait bound `T: std::cmp::Eq` is not satisfied
--> /Users/ania/.cargo/registry/src/github.com-1ecc6299db9ec823/ahash-0.6.3/src/hash_set.rs:49:18
|
42 | impl<T, S> AHashSet<T, S> where S: BuildHasher {
| - help: consider further restricting type parameter `T`: `, T: std::cmp::Eq`
...
49 | AHashSet(HashSet::with_capacity_and_hasher(capacity, hash_builder))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::cmp::Eq` is not implemented for `T`
|
= note: required by `std::collections::HashSet::<T, S>::with_capacity_and_hasher`
error[E0277]: the trait bound `T: std::hash::Hash` is not satisfied
--> /Users/ania/.cargo/registry/src/github.com-1ecc6299db9ec823/ahash-0.6.3/src/hash_set.rs:49:18
|
42 | impl<T, S> AHashSet<T, S> where S: BuildHasher {
| - help: consider further restricting type parameter `T`: `, T: std::hash::Hash`
...
49 | AHashSet(HashSet::with_capacity_and_hasher(capacity, hash_builder))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::hash::Hash` is not implemented for `T`
|
= note: required by `std::collections::HashSet::<T, S>::with_capacity_and_hasher`
error[E0599]: no method named `fmt` found for struct `std::collections::HashSet<T, S>` in the current scope
--> /Users/ania/.cargo/registry/src/github.com-1ecc6299db9ec823/ahash-0.6.3/src/hash_set.rs:217:16
|
217 | self.0.fmt(fmt)
| ^^^ method not found in `std::collections::HashSet<T, S>`
|
= note: the method `fmt` exists but the following trait bounds were not satisfied:
`std::collections::HashSet<T, S> : std::fmt::Debug`
error[E0277]: the trait bound `T: std::cmp::Eq` is not satisfied
--> /Users/ania/.cargo/registry/src/github.com-1ecc6299db9ec823/ahash-0.6.3/src/hash_set.rs:274:18
|
270 | impl<T> Default for AHashSet<T, RandomState> {
| - help: consider restricting this bound: `T: std::cmp::Eq`
...
274 | AHashSet(HashSet::default())
| ^^^^^^^^^^^^^^^^^^ the trait `std::cmp::Eq` is not implemented for `T`
|
= note: required because of the requirements on the impl of `std::default::Default` for `std::collections::HashSet<T, random_state::RandomState>`
= note: required by `std::default::Default::default`
error[E0277]: the trait bound `T: std::hash::Hash` is not satisfied
--> /Users/ania/.cargo/registry/src/github.com-1ecc6299db9ec823/ahash-0.6.3/src/hash_set.rs:274:18
|
270 | impl<T> Default for AHashSet<T, RandomState> {
| - help: consider restricting this bound: `T: std::hash::Hash`
...
274 | AHashSet(HashSet::default())
| ^^^^^^^^^^^^^^^^^^ the trait `std::hash::Hash` is not implemented for `T`
|
= note: required because of the requirements on the impl of `std::default::Default` for `std::collections::HashSet<T, random_state::RandomState>`
= note: required by `std::default::Default::default`
error: aborting due to 20 previous errors
Some errors have detailed explanations: E0277, E0599.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `ahash`.
warning: build failed, waiting for other jobs to finish...
error: failed to compile `flamegraph v0.4.0`, intermediate artifacts can be found at `/var/folders/fl/dr1_kh0d5xjdsxcd5x8pvb_w0000gn/T/cargo-installllCFHI`
Caused by:
build failed
Anyone can help with that? Is there additional stuff I should install first?
flamegraph appears to be taking advantage of relaxed trait bounds on Hashmap, which were released in Rust 1.43:
Version 1.43.0 (2020-04-23)
…
Libraries
…
Relaxed some of the trait bounds on HashMap and HashSet.
If you want to use flamegraph, you therefore must update your version of rustc.
I try to write generic method and specify some type T: num_traits::float::Float from num-traits crate. How should I ask &T to implement basic numeric methods such as multiplication?
use splines::{Interpolation, Key, Spline, interpolate};
use num_traits::{float::Float, identities};
use conv::prelude::*;
struct Interpolator<T>
where T: Float + interpolate::One + interpolate::Trigo {
target_x: Vec<T>,
}
impl<T> Interpolator<T>
where T: Float + interpolate::One + interpolate::Trigo {
fn interpolate<U>(&self, x: &[T], y: &[U]) -> Vec<U>
where U: Float + identities::Zero {
assert_eq!(x.len(), y.len());
let key_iter = x.iter().zip(y).map(|(x, y)| Key::new(x, y, Interpolation::Linear));
let spline = Spline::from_iter(key_iter);
let result: Vec<U> = self.target_x.iter().map(|x| spline.sample(x).unwrap_or(identities::zero())).collect();
result
}
}
fn main() {
}
Cargo.toml
[package]
name = "name"
version = "0.1.0"
edition = "2018"
[dependencies]
conv = "0.3.2"
splines = "1.0.0-rc.3"
num-traits = "0.2"
Compilation errors:
error[E0277]: the trait bound `&T: splines::interpolate::One` is not satisfied
--> src/main.rs:18:66
|
18 | let result: Vec<U> = self.target_x.iter().map(|x| spline.sample(x).unwrap_or(identities::zero())).collect();
| ^^^^^^ the trait `splines::interpolate::One` is not implemented for `&T`
error[E0277]: the trait bound `&T: splines::interpolate::Trigo` is not satisfied
--> src/main.rs:18:66
|
18 | let result: Vec<U> = self.target_x.iter().map(|x| spline.sample(x).unwrap_or(identities::zero())).collect();
| ^^^^^^ the trait `splines::interpolate::Trigo` is not implemented for `&T`
error[E0277]: cannot multiply `&T` to `&T`
--> src/main.rs:18:66
|
18 | let result: Vec<U> = self.target_x.iter().map(|x| spline.sample(x).unwrap_or(identities::zero())).collect();
| ^^^^^^ no implementation for `&T * &T`
|
= help: the trait `std::ops::Mul` is not implemented for `&T`
= help: consider adding a `where &T: std::ops::Mul` bound
error[E0277]: cannot divide `&T` by `&T`
--> src/main.rs:18:66
|
18 | let result: Vec<U> = self.target_x.iter().map(|x| spline.sample(x).unwrap_or(identities::zero())).collect();
| ^^^^^^ no implementation for `&T / &T`
|
= help: the trait `std::ops::Div` is not implemented for `&T`
= help: consider adding a `where &T: std::ops::Div` bound
error[E0277]: the trait bound `&U: splines::interpolate::Interpolate<&T>` is not satisfied
--> src/main.rs:18:66
|
18 | let result: Vec<U> = self.target_x.iter().map(|x| spline.sample(x).unwrap_or(identities::zero())).collect();
| ^^^^^^ the trait `splines::interpolate::Interpolate<&T>` is not implemented for `&U`
error[E0277]: cannot add `&T` to `&T`
--> src/main.rs:18:66
|
18 | let result: Vec<U> = self.target_x.iter().map(|x| spline.sample(x).unwrap_or(identities::zero())).collect();
| ^^^^^^ no implementation for `&T + &T`
|
= help: the trait `std::ops::Add` is not implemented for `&T`
= help: consider adding a `where &T: std::ops::Add` bound
= note: required because of the requirements on the impl of `splines::interpolate::Additive` for `&T`
error[E0277]: cannot subtract `&T` from `&T`
--> src/main.rs:18:66
|
18 | let result: Vec<U> = self.target_x.iter().map(|x| spline.sample(x).unwrap_or(identities::zero())).collect();
| ^^^^^^ no implementation for `&T - &T`
|
= help: the trait `std::ops::Sub` is not implemented for `&T`
= help: consider adding a `where &T: std::ops::Sub` bound
= note: required because of the requirements on the impl of `splines::interpolate::Additive` for `&T`
error[E0277]: the trait bound `&U: num_traits::identities::Zero` is not satisfied
--> src/main.rs:18:86
|
18 | let result: Vec<U> = self.target_x.iter().map(|x| spline.sample(x).unwrap_or(identities::zero())).collect();
| ^^^^^^^^^^^^^^^^ the trait `num_traits::identities::Zero` is not implemented for `&U`
|
= note: required by `num_traits::identities::zero`
error[E0277]: a collection of type `std::vec::Vec<U>` cannot be built from an iterator over elements of type `&U`
--> src/main.rs:18:107
|
18 | let result: Vec<U> = self.target_x.iter().map(|x| spline.sample(x).unwrap_or(identities::zero())).collect();
| ^^^^^^^ a collection of type `std::vec::Vec<U>` cannot be built from `std::iter::Iterator<Item=&U>`
|
= help: the trait `std::iter::FromIterator<&U>` is not implemented for `std::vec::Vec<U>`
error: aborting due to 9 previous errors
For more information about this error, try `rustc --explain E0277`.
error: Could not compile `trait_generic`.
Maybe you could just consider using Key::new(*x, *y, Interpolation::Linear) and spline.sample(*x)?
This question already has answers here:
How can I have a collection of objects that differ by their associated type?
(3 answers)
Vector of objects belonging to a trait
(3 answers)
Closed 4 years ago.
I am building a command-line application. I have the following trait:
trait ValidatedCommand {
type Output;
fn run() -> Result<Self::Output, std::io::Error>;
}
and I have the following two implementations for it:
struct First;
impl ValidatedCommand for First {
type Output = i32;
fn run() -> Result<i32, std::io::Error> {
Ok(1)
}
}
impl First {
fn new() -> First {
First {}
}
}
struct Second;
impl ValidatedCommand for Second {
type Output = String;
fn run() -> Result<String, std::io::Error> {
Ok(String::from("hello"))
}
}
impl Second {
fn new() -> Second {
Second {}
}
}
Both structs implement the trait, one returning a String and the other an i32.
I'm trying to create a Vec of that trait, but I'm not sure how to go about it. I tried the following:
fn main() {
let commands: Vec<dyn ValidatedCommand> = vec![First::new(), Second::new()];
playground.
which errors with
error[E0191]: the value of the associated type `Output` (from the trait `ValidatedCommand`) must be specified
--> src/main.rs:33:23
|
2 | type Output;
| ------------ `Output` defined here
...
33 | let commands: Vec<dyn ValidatedCommand> = vec![First::new(), Second::new()];
| ^^^^^^^^^^^^^^^^^^^^ associated type `Output` must be specified
error[E0277]: the size for values of type `dyn ValidatedCommand` cannot be known at compilation time
--> src/main.rs:33:19
|
33 | let commands: Vec<dyn ValidatedCommand> = vec![First::new(), Second::new()];
| ^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `dyn ValidatedCommand`
= note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= note: required by `std::vec::Vec`
error[E0038]: the trait `ValidatedCommand` cannot be made into an object
--> src/main.rs:33:19
|
33 | let commands: Vec<dyn ValidatedCommand> = vec![First::new(), Second::new()];
| ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `ValidatedCommand` cannot be made into an object
|
= note: method `run` has no receiver
error[E0308]: mismatched types
--> src/main.rs:33:52
|
33 | let commands: Vec<dyn ValidatedCommand> = vec![First::new(), Second::new()];
| ^^^^^^^^^^^^ expected trait ValidatedCommand, found struct `First`
|
= note: expected type `dyn ValidatedCommand`
found type `First`
error[E0277]: the size for values of type `dyn ValidatedCommand` cannot be known at compilation time
--> src/main.rs:33:47
|
33 | let commands: Vec<dyn ValidatedCommand> = vec![First::new(), Second::new()];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `dyn ValidatedCommand`
= note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= note: required by `std::slice::<impl [T]>::into_vec`
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
error[E0277]: the size for values of type `dyn ValidatedCommand` cannot be known at compilation time
--> src/main.rs:33:47
|
33 | let commands: Vec<dyn ValidatedCommand> = vec![First::new(), Second::new()];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `dyn ValidatedCommand`
= note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= note: slice and array elements must have `Sized` type
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
error[E0038]: the trait `ValidatedCommand` cannot be made into an object
--> src/main.rs:33:47
|
33 | let commands: Vec<dyn ValidatedCommand> = vec![First::new(), Second::new()];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `ValidatedCommand` cannot be made into an object
|
= note: method `run` has no receiver
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
error[E0277]: the size for values of type `dyn ValidatedCommand` cannot be known at compilation time
--> src/main.rs:33:47
|
33 | let commands: Vec<dyn ValidatedCommand> = vec![First::new(), Second::new()];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `dyn ValidatedCommand`
= note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= note: required by `std::vec::Vec`
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
error[E0038]: the trait `ValidatedCommand` cannot be made into an object
--> src/main.rs:33:47
|
33 | let commands: Vec<dyn ValidatedCommand> = vec![First::new(), Second::new()];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `ValidatedCommand` cannot be made into an object
|
= note: method `run` has no receiver
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
I am thinking that maybe there is a way to make use of the associated type, but I have no idea how I might go about it.
Is there a way to get this vector creation to compile?
This question already has answers here:
Vector of objects belonging to a trait
(3 answers)
Closed 4 years ago.
I have the following code:
use std::collections::HashMap;
trait T: Sized {}
struct A;
impl T for A {}
fn main() {
let h: HashMap<String, T>;
}
But the compiler complains:
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`
I don't understand the error messages, because I've marked my trait T as Sized. Am I missing something?
because I've marked my trait T as Sized
No, you haven't. You've said that any type that implements T must be Sized. The trait itself is still unsized. You either need a trait object (e.g. Box<T>) or some kind of generic (which you cannot do in this context).