You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用trunk serve运行Stylist 0.12.1时编译失败求助

Rust/Yew项目中Stylist 0.12.1配合Trunk编译报错的问题排查与修复

问题场景

在Rust/Yew项目中使用Stylist 0.12.1时,cargo build可正常编译,但执行trunk serve会触发类型不匹配及错误转换类的编译错误。

依赖配置

Cargo.toml中Stylist的配置:

stylist = { version = "0.12.1", features = ["yew"] }

报错信息

执行trunk serve时的错误输出:

error[E0308]: mismatched types
   --> /home/user/.cargo/registry/src/index.crates.io-6f17d22bba15001f/stylist-0.12.1/src/manager/mod.rs:244:38
    |
244 |         .map_err(|e| Error::Web(Some(e)))
    |                                 ---- ^ expected `()`, found `JsValue`
    |                                 |
    |                                 arguments to this enum variant are incorrect
    |
help: the type constructed contains `JsValue` due to the type of the argument passed
   --> /home/user/.cargo/registry/src/index.crates.io-6f17d22bba15001f/stylist-0.12.1/src/manager/mod.rs:244:33
    |
244 |         .map_err(|e| Error::Web(Some(e)))
    |                                 ^^^^^-^
    |                                      |
    |                                      this argument influences the type of `Some`
note: tuple variant defined here
   --> /home/user/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/option.rs:571:5
    |
571 |     Some(#[stable(feature = "rust1", since = "1.0.0")] T),
    |     ^^^^

error[E0277]: `?` couldn't convert the error to `()`
   --> /home/user/.cargo/registry/src/index.crates.io-6f17d22bba15001f/stylist-0.12.1/src/manager/mod.rs:255:91
    |
255 |             if let Some(m) = document.query_selector(&format!("style[data-style={}]", id))? {
    |                                                                                           ^ the trait `From<JsValue>` is not implemented for `()`
    |
    = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
    = help: the following other types implement trait `From<T>`:
              <(T, T) as From<[T; 2]>>
              <(T, T, T) as From<[T; 3]>>
              <(T, T, T, T) as From<[T; 4]>>
              <(T, T, T, T, T) as From<[T; 5]>>
              <(T, T, T, T, T, T) as From<[T; 6]>>
              <(T, T, T, T, T, T, T) as From<[T; 7]>>
              <(T, T, T, T, T, T, T, T) as From<[T; 8]>>
              <(T, T, T, T, T, T, T, T, T) as From<[T; 9]>>
            and 4 others
    = note: required for `Result<(), ()>` to implement `FromResidual<Result<Infallible, JsValue>>`

error[E0277]: `?` couldn't convert the error to `()`
   --> /home/user/.cargo/registry/src/index.crates.io-6f17d22bba15001f/stylist-0.12.1/src/manager/mod.rs:257:44
    |
257 |                     parent.remove_child(&m)?;
    |                                            ^ the trait `From<JsValue>` is not implemented for `()`
    |
    = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
    = help: the following other types implement trait `From<T>`:
              <(T, T) as From<[T; 2]>>
              <(T, T, T) as From<[T; 3]>>
              <(T, T, T, T) as From<[T; 4]>>
              <(T, T, T, T, T) as From<[T; 5]>>
              <(T, T, T, T, T, T) as From<[T; 6]>>
              <(T, T, T, T, T, T, T) as From<[T; 7]>>
              <(T, T, T, T, T, T, T, T) as From<[T; 8]>>
              <(T, T, T, T, T, T, T, T, T) as From<[T; 9]>>
            and 4 others
    = note: required for `Result<(), ()>` to implement `FromResidual<Result<Infallible, JsValue>>`

Some errors have detailed explanations: E0277, E0308.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `stylist` (lib) due to 3 previous errors
2023-09-12T21:39:07.427662Z ERROR ❌ error
error from HTML pipeline

环境版本信息

rustc -vV
rustc 1.71.1 (eb26296b5 2023-08-03)
binary: rustc
commit-hash: eb26296b556cef10fb713a38f3d16b9886080f26
commit-date: 2023-08-03
host: x86_64-unknown-linux-gnu
release: 1.71.1
LLVM version: 16.0.5

cargo -vV
cargo 1.71.1 (7f1d04c00 2023-07-29)
release: 1.71.1
commit-hash: 7f1d04c0053083b98fa50b69b6f56e339b0556a8
commit-date: 2023-07-29
host: x86_64-unknown-linux-gnu
libgit2: 1.6.4 (sys:0.17.1 vendored)
libcurl: 8.0.1-DEV (sys:0.4.61+curl-8.0.1 vendored ssl:OpenSSL/1.1.1t)
ssl: OpenSSL 1.1.1t  7 Feb 2023
os: Debian n/a (trixie) [64-bit]

trunk -vV
trunk 0.17.5

复现步骤

执行以下bash命令可复现问题:

mkdir test
cd test

cat > Cargo.toml<<EOF
[workspace]
members = [
    "test_web"
]
EOF

cargo new test_web
cd test_web
cargo add wasm-bindgen@0.2.87
cargo add web-sys@0.3.64 -F "HtmlInputElement"
cargo add yew@0.20.0 -F csr
cargo add yew-router@0.17.0
cargo add stylist@0.12.1 -F yew
cargo install trunk --version 0.17.5
touch index.html

trunk serve  # 执行此命令会抛出上述错误

问题原因与修复方案

问题原因

Stylist 0.12.1版本存在代码bug:在manager/mod.rs中,Error::Web枚举变体定义为接收Option<()>类型,但实际代码传入了Option<JsValue>,导致类型不匹配;同时,使用?运算符的函数返回类型为Result<(), ()>,无法将JsValue错误转换为(),触发E0277错误。该问题在Stylist后续版本中已被修复。

修复方案

  1. 升级Stylist版本:将Stylist升级至0.13.0及以上版本,修改Cargo.toml中的依赖配置:

    stylist = { version = "0.13.0", features = ["yew"] }
    

    执行cargo update更新依赖。

  2. 验证修复:重新执行trunk serve,项目即可正常编译运行。


内容的提问来源于stack exchange,提问作者huber.duber

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 20:14:51