htmlcssrustrust-cratesyew

&str not converted into StyleSource implicitly in rust stylist


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.


Solution

  • 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.