&str not converted into StyleSource implicitly in rust stylist

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

&str not converted into StyleSource implicitly in rust stylist

问题

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

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

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

以下是我的代码。

  1. use stylist::{yew::styled_component, Style};
  2. #[styled_component(App)]
  3. pub fn app() -> Html {
  4. let style_str = "background-color: red;";
  5. let stylesheet = Style::new(style_str).unwrap();
  6. html! {
  7. <div class={stylesheet}>
  8. <h1>{"Hello World using stylesheet!"}</h1>
  9. <p>{"some more text..."}</p>
  10. </div>
  11. }
  12. }

我正在尝试使用在线教程进行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:

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

  1. use stylist::{yew::styled_component, Style};
  2. #[styled_component(App)]
  3. pub fn app() -&gt; Html {
  4. let style_str = &quot;background-color: red;&quot;;
  5. let stylesheet = Style::new(style_str).unwrap();
  6. html! {
  7. &lt;div class={stylesheet}&gt;
  8. &lt;h1&gt;{&quot;Hello World using stylesheet!&quot;}&lt;/h1&gt;
  9. &lt;p&gt;{&quot;some more text...&quot;}&lt;/p&gt;
  10. &lt;/div&gt;
  11. }
  12. }

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中启用此功能。

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

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

英文:

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

  1. [dependencies]
  2. 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:

确定