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

Rust !ink智能合约教程测试报错求助:scale-info编译失败

问题:ink合约执行cargo test时scale-info编译报错(E0282)

我是Rust新手,正在学习!ink智能合约,跟着官方教程创建flipper合约后,执行cargo test时遇到编译错误,错误信息如下:

Compiling anstyle-parse v0.2.4
   Compiling const_env v0.1.2
   Compiling hyper v1.3.1
error[E0282]: type annotations needed
  --> /home/hago/.cargo/registry/src/index.crates.io-6f17d22bba15001f/scale-info-2.11.2/src/form.rs:64:39
   |
64 | #[cfg_attr(feature = "schema", derive(JsonSchema))]
   |                                       ^^^^^^^^^^ cannot infer type
   |
   = note: this error originates in the derive macro `JsonSchema` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0282]: type annotations needed
  --> /home/hago/.cargo/registry/src/index.crates.io-6f17d22bba15001f/scale-info-2.11.2/src/form.rs:81:39
   |
81 | #[cfg_attr(feature = "schema", derive(JsonSchema))]
   |                                       ^^^^^^^^^^ cannot infer type
   |
   = note: this error originates in the derive macro `JsonSchema` (in Nightly builds, run with -Z macro-backtrace for more info)

   Compiling wasm-opt-cxx-sys v0.116.0
   Compiling darling v0.20.8
   Compiling jsonrpsee-types v0.22.5
   Compiling scale-typegen v0.1.1
   Compiling tower v0.4.13
For more information about this error, try `rustc --explain E0282`.
error: could not compile `scale-info` (lib) due to 2 previous errors
warning: build failed, waiting for other jobs to finish...

解决方案

  • 检查并切换Rust版本:ink合约对Rust版本有严格兼容要求,建议使用官方教程指定的稳定版(如1.75+)。执行rustup show查看当前版本,若不匹配,用rustup install <目标版本>安装后,通过rustup default <目标版本>切换。
  • 清理编译缓存:执行cargo clean清除旧的编译产物和缓存,再重新运行cargo test。
  • 锁定兼容的依赖版本:在项目Cargo.toml中明确指定scale-info的稳定版本,例如scale-info = "2.10.0",或执行cargo update将所有依赖更新到与当前ink版本兼容的最新版。
  • 禁用schema feature(谨慎使用):如果不需要生成schema功能,可在Cargo.toml中给scale-info禁用该feature:
    scale-info = { version = "2.11.2", default-features = false }
    
    注意:此操作可能影响其他依赖的功能,需确认项目不需要schema相关特性后再使用。

内容的提问来源于stack exchange,提问作者StackMyFlow

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 10:12:47