help clause points to adding where clause on impl when it should be on function · Issue #104089 · rust-lang/rust (original) (raw)
Navigation Menu
- Explore
- Pricing
Provide feedback
Saved searches
Use saved searches to filter your results more quickly
Appearance settings
Description
Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=e5d9c21342f0c0824ddba9445daf7cb1
struct A { x: String }
struct B { x: String }
impl From for B { fn from(a: A) -> Self { B { x: a.x } } }
impl B { pub fn from_many<T: Into + Clone>(v: Vec) -> Self { B { x: v.iter().map(|e| B::from(e.clone()).x).collect::<Vec>().join(" ") } } }
fn main() { let b: B = B { x: "foobar".to_string() }; let a: A = A { x: "frob".to_string() }; let ab: B = a.into(); println!("Hello, {}!", ab.x); }
The current output is:
error[[E0277]](https://doc.rust-lang.org/stable/error-index.html#E0277): the trait bound `B: From<T>` is not satisfied
--> src/main.rs:17:41
|
17 | B { x: v.iter().map(|e| B::from(e.clone()).x).collect::<Vec<String>>().join(" ") }
| ------- ^^^^^^^^^ the trait `From<T>` is not implemented for `B`
| |
| required by a bound introduced by this call
|
help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement
|
15 | impl B where B: From<T> {
| ++++++++++++++++
Ideally the output should look like:
...
help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement
|
pub fn from_many<T: Into<B> + Clone>(v: Vec<T>) -> Self
where B: From<T> {
...
}
link to stack overflow: https://stackoverflow.com/questions/74342563/convert-vector-of-type-a-to-type-b-where-a-is-convertible-to-b?noredirect=1#comment131245422_74342563