&str not converted into StyleSource implicitly in rust stylist

huangapple go评论68阅读模式
英文:

&str not converted into StyleSource implicitly in rust stylist

问题

我正在尝试在Rust中首次使用yew和stylist。根据教程,我应该能够将&str转换为所需的类型,但它产生了以下错误:

未为`StyleSource`实现`From<&str>`特性

错误信息可参考下面的图片。
error

以下是我的代码。

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>
    }
}

我正在尝试使用在线教程进行Rust前端开发。为此,我使用了yewstylist依赖项来处理HTML和CSS代码。根据文档,应该能够使用Style::new(name_of_variable)来运行解析后的css文件。但它抛出了上述错误。

英文:

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

the trait `From&lt;&amp;str&gt;` 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() -&gt; Html {    
    let style_str = &quot;background-color: red;&quot;;
    let stylesheet = Style::new(style_str).unwrap();
    html! {
            &lt;div class={stylesheet}&gt;
                &lt;h1&gt;{&quot;Hello World using stylesheet!&quot;}&lt;/h1&gt;
                &lt;p&gt;{&quot;some more text...&quot;}&lt;/p&gt;
            &lt;/div&gt;
    }
}

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

得分: 1

String conversions是通过parsercargo功能实现的。您可以在Cargo.toml中启用此功能。

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

然后,您的代码应该按原样工作。

英文:

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

[dependencies]
stylist = { version = &quot;0.12.1&quot;, features = [&quot;parser&quot;] }

Then, your code should work as-is.

huangapple
  • 本文由 发表于 2023年7月28日 02:33:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76782549.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定