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

嵌入式Rust项目#![no_std]下找不到test crate问题求助

解决嵌入式Rust #![no_std]下"can't find crate for test"错误

错误原因拆解

  1. "can't find crate for test":#![no_std]环境不包含标准库的test crate,而Rust默认测试框架依赖这个crate,编译测试目标时就会触发该错误。
  2. 手动添加core失败:core是Rust内置的核心库,随工具链自带,不需要在Cargo.toml里声明依赖,手动添加自然会提示找不到。

具体解决步骤

1. 禁用默认测试目标(最简临时方案)

如果暂时不需要测试,直接在Cargo.toml中添加以下配置,让Cargo不再构建测试目标:

[profile.dev]
test = false

[profile.release]
test = false

2. 确保交叉编译工具链配置正确

嵌入式Rust需要针对目标平台的交叉编译工具链,以ARM平台为例:

  • 先安装对应目标的工具链:
    rustup target add arm-none-eabi
    
  • 在Cargo.toml中添加目标平台的编译配置(根据你的硬件调整):
    [package]
    name = "your-embedded-project"
    version = "0.1.0"
    edition = "2021"
    
    [profile.dev]
    panic = "abort"
    
    [profile.release]
    panic = "abort"
    
    # link.x是嵌入式常用链接脚本,需确保已引入(比如通过cortex-m crate)
    [target.arm-none-eabi]
    rustflags = [
      "-C", "link-arg=-Tlink.x",
    ]
    

3. 确认panic handler正确

你main.rs里的panic handler完全符合要求,无需修改:

#![no_std]
#![no_main]

use core::panic::PanicInfo;

#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
    loop {}
}

后续若需测试

如果之后要为嵌入式项目写测试,可以使用专门的嵌入式测试框架(比如cortex-m-rt配合cortex-m-semihosting),不过这需要额外配置链接脚本和测试入口,等需要时再针对性调整即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 16:27:06