When adding `#![no_std]` to a library, are there any disadvantages or complications for the users of that library?
我写了一个Rust库。我听说了
但是我不知道这会对我的图书馆用户产生怎样的影响。当然,我希望通过使用
如果使用
For example, are they forced to also use
#![no_std] ?
一点也不。依赖的板条箱(即将消耗您的板条箱的板条箱/项目)将知道找到您的依赖项所需要的
准备了依赖关系,则与您的依赖关系兼容的板条箱集应始终是一个超集。
KodrAus / rust-nostd的自述文件(在库中使用和测试
The current design of Rust's standard library is split into a few layers, each building on the assumed platform capabilities of the one below. There's:
std : the full standard library assumes the presence of threads, a filesystem, and networking. [...]alloc : the collections layer builds on the core by assuming runtime support for dynamic memory allocation.core : the core layer makes no (well, not very many) assumptions about the > underlying platform. Just about any target that can run Rust code is supported by core.So when you're designing your library you can make it maximally portable by targeting the lowest layer of the standard library that you can.
某些package箱将
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #![cfg_attr(not(feature ="std"), no_std)] #[cfg(feature ="std")] extern crate core; #[cfg(feature ="alloc")] extern crate alloc; pub fn foo_into_slice(slice: &mut [u8]) { unimplemented!() } /// Vec requires alloc #[cfg(feature ="alloc")] use alloc::vec::Vec; #[cfg(feature ="alloc")] pub fn foo_into_vec(vec: &mut Vec<u8>) { unimplemented!() } |