用途:指导代理进行 #![no_std] Rust 开发,包括 core 与 alloc、std 的区别,实现自定义全局分配器,为嵌入式目标选择 panic 处理器,以及在本机测试 no_std crate 的策略。适用于编写或调试 #![no_std] Rust 代码——嵌入式目标的库 crate,或无法链接 std 的裸机固件。完整的嵌入式工作流(probe-rs 烧录、defmt 日志、RTIC)见 skills/embedded/embedded-rust;交叉编译目标设置见 skills/rust/rust-cross。本技能聚焦 no_std/core/alloc 边界与 panic 处理器选择。
主要能力:core 无需操作系统与堆,提供基础类型、trait、iter、fmt、mem、ptr、slice、option、result;alloc 无需操作系统但需堆与全局分配器,提供 Vec、String、Box、Arc、Rc、HashMap;std 需要操作系统与堆,包含全部 core 与 alloc 及线程、文件、套接字等。在 no_std 中使用 alloc 需提供全局分配器,常见 crate 有 linked-list-allocator、buddy-alloc、dlmalloc、talc。no_std 必须提供 panic 处理器,可循环停机,或用 defmt 打印并触发硬件异常,也可使用 panic-halt、panic-reset、panic-probe 等 crate。可编写兼容有/无 alloc 的可移植库,用 feature 门控 alloc API,核心 API 返回借用切片。
使用方式:crate 以 #![no_std] 声明,按需 extern crate alloc,用 #[cfg(feature = "alloc")] 门控堆 API;Cargo.toml 中定义 alloc 等 feature 并仅依赖 no_std 兼容 crate。测试时可用 #![cfg_attr(not(test), no_std)] 在测试模式允许 std,或执行 cargo test --target x86_64-unknown-linux-gnu;用 cargo check --target thumbv7em-none-eabihf 和 cargo build 检查 no_std 合规性。
限制:裸机目标无测试运行器,cargo test 对 thumb 目标会失败,需用 defmt-test 或 probe-run 做板上测试,或在本机做架构无关的纯逻辑测试。init_heap 需在 BSS/data 初始化后调用。相关技能:embedded-rust、rust-cross、rust-unsafe、linker-scripts。收费、价格、平台与效果信息暂未提供。