rustrust-no-std

Why can a no_std crate depend on a crate that uses std?


In the example, hs reexport HashSet from std. But it compiles without error or warning. Why?

#![no_std]

pub use hs::HashSet;

pub fn new() -> HashSet<usize> {
    HashSet::new()
}

pub fn insert(a: &mut HashSet<usize>, v: usize) {
    a.insert(v);
}

Solution

  • Well #![no_std] just means that you don't include std by default. It doesn't mean you can't explicitly or implicitly (i.e. through other crates) still include std. In other words #![no_std] does not prohibit using std but it disables using it by default.

    For example this works, too:

    #![no_std]
    extern crate std;
    fn main() {
        std::println!("hello!");
    }