The trait bound is not satisfied.. What to do?

Is the information in error message enough to look a solution?

    let clk/* :Callback<MouseEvent>  */   =Callback::from(move|_|{
        let inp   =inp_rf.cast::<web_sys::HtmlInputElement>().unwrap();
        let gs    =inp.value().to_lowercase();

        if gs==name {  gs_st.set(Some(Guess::Correct));    }
        else        {  gs_st.set(Some(Guess::NoNoNo ));    }

        web_sys::console::log_1(&gs.into());
    });

error[E0277]: the trait bound `yew::Callback<_>: IntoPropValue<Option<AttrValue>>` is not satisfied
   --> src\main.rs:112:21
    |
112 |            <button {clk}>{"submit"}</button>
    |                     ^^^ the trait `IntoPropValue<Option<AttrValue>>` is not implemented for `yew::Callback<_>`
    |
    = help: the following other types implement trait `IntoPropValue<T>`:
              <&'static str as IntoPropValue<AttrValue>>
              <&'static str as IntoPropValue<Classes>>
              <&'static str as IntoPropValue<Option<AttrValue>>>
              <&'static str as IntoPropValue<Option<std::string::String>>>
              <&'static str as IntoPropValue<std::string::String>>
              <&T as IntoPropValue<Option<T>>>
              <&T as IntoPropValue<T>>
              <Classes as IntoPropValue<AttrValue>>
            and 6 others

For more information about this error, try `rustc --explain E0277`.

/////////////////////////////////
rustc --explain E0277:

You tried to use a type which doesn't implement some trait in a place which
expected that trait.

Erroneous code example:

// here we declare the Foo trait with a bar method
trait Foo {
    fn bar(&self);
}

// we now declare a function which takes an object implementing the Foo trait
fn some_func<T: Foo>(foo: T) {
    foo.bar();
}

fn main() {
    // we now call the method with the i32 type, which doesn't implement
    // the Foo trait
    some_func(5i32); // error: the trait bound `i32 : Foo` is not satisfied
}

In order to fix this error, verify that the type you're using does implement
the trait. Example:

trait Foo {
    fn bar(&self);
}

// we implement the trait on the i32 type
impl Foo for i32 {
    fn bar(&self) {}
}

fn some_func<T: Foo>(foo: T) {
    foo.bar(); // we can now use this method since i32 implements the
               // Foo trait
}

fn main() {
    some_func(5i32); // ok!
}

Or in a generic context, an erroneous code example would look like:

fn some_func<T>(foo: T) {
    println!("{:?}", foo); // error: the trait `core::fmt::Debug` is not
                           //        implemented for the type `T`
}

fn main() {
    // We now call the method with the i32 type,
    // which *does* implement the Debug trait.
    some_func(5i32);
}

Note that the error here is in the definition of the generic function. Although
we only call it with a parameter that does implement Debug, the compiler
still rejects the function. It must work with all possible input types. In
order to make this example compile, we need to restrict the generic type we're
accepting:

use std::fmt;

// Restrict the input type to types that implement Debug.
fn some_func<T: fmt::Debug>(foo: T) {
    println!("{:?}", foo);
}

fn main() {
    // Calling the method is still fine, as i32 implements Debug.
    some_func(5i32);

    // This would fail to compile now:
    // struct WithoutDebug;
    // some_func(WithoutDebug);
}

Rust only looks at the signature of the called function, as such it must
already specify all requirements that will be used for every type parameter.

( end of rustc --explain E0277 )
//////////////////////////////////////////////

Here is that trait:
https://docs.rs/yew/0.19.1/yew/html/trait.IntoPropValue.html

In this case, you probably want to look at yew documentation, to see how it expects these callbacks to be set. According to this documentation, you want to use something like <button onclick={clk}>, not <button {clk}>.

3 Likes

2022-07-08T11:11:15.708472Z INFO success

Thank You, eagle-eyed friend, very much. I was seeking in traits and how and where to implement its..

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.