Skip to content

help clause points to adding where clause on impl when it should be on function #104089

@OneRaynyDay

Description

@OneRaynyDay

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<A> for B {
    fn from(a: A) -> Self {
        B { x: a.x }
    }
}

impl B {
    pub fn from_many<T: Into<B> + Clone>(v: Vec<T>) -> Self {
        B { x: v.iter().map(|e| B::from(e.clone()).x).collect::<Vec<String>>().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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.D-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions