0

I am trying out yew and stylist for the first time in rust. As per the tutorials, i should be able to convert &str into the required type, but it produces the error given as:

the trait `From<&str>` is not implemented for `StyleSource`

The error is shown in the image for reference. error

Following is my code.

use stylist::{yew::styled_component, Style};

#[styled_component(App)]
pub fn app() -> Html {    
    let style_str = "background-color: red;";
    let stylesheet = Style::new(style_str).unwrap();
    html! {
            <div class={stylesheet}>
                <h1>{"Hello World using stylesheet!"}</h1>
                <p>{"some more text..."}</p>
            </div>
    }
}

I was trying frontend development in RUST using online tutorials. To do this I was using yew and stylist dependencies to work with html and css codes. As per documentation, a parsed css file should be able to run using Style::new(name_of_variable). But it is throwing the stated error.

  • 1
    Please post the full error from `cargo check` **as text**. See [Why should I not upload images of code/data/errors?](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors) – Chayim Friedman Jul 27 '23 at 18:47
  • It says this on the [front page of stylist's documentation](https://docs.rs/stylist/latest/stylist/index.html#style-api): "If you want to parse a string into a style at runtime, you need to enable the parser feature." – drewtato Jul 27 '23 at 18:50
  • Thanks for the image info. Will keep that in mind. @ChayimFriedman – xD-prateek Jul 28 '23 at 06:15
  • Yes including "parser" feature along with stylist worked. Thanks a lot @drewtato – xD-prateek Jul 28 '23 at 06:17
  • Cool, I've turned it into an answer. – drewtato Jul 28 '23 at 07:35

1 Answers1

1

String conversions are behind the parser cargo feature. You can enable this feature in Cargo.toml.

[dependencies]
stylist = { version = "0.12.1", features = ["parser"] }

Then, your code should work as-is.

drewtato
  • 6,783
  • 1
  • 12
  • 17