Hacker News new | past | comments | ask | show | jobs | submit login

> In practice you can't combine no_std and std within a single project anyway

You can with Rust features. It allows a library to conditionally compile certain parts of the code, and users of the library can decide which features to enable.

If a library only uses stdlib in code gated by #[feature(std)], users can disable that flag, and use the library in no_std contexts.




Interesting. I have no rust experience yet, so i wonder: while that sounds very cool, how does it look like in the wild? To me it seems like this could quickly turn into a nightmare, if the code is not very well organized / structured. In reality shouldn't this rather be two separate libs?

But maybe i should first learn rust before asking.


It's definitely a tool that must be used carefully, and why informal rules like "features should be strictly additive" exist. I'd say they are very commonly used by large libraries, for example, here's the list of features for Axum, (arguably) Rust's most popular http server: https://docs.rs/axum/latest/axum/#feature-flags


Anecdotally, the use cases I've had are all clearly in one category or the other: I'm either building firmware for an embedded device, or building a PC application. I'm curious about the overlap cases.


You're right that an application project (a `binary crate` in Rust parlance) for embedded firmware versus a PC application is very different. But library crates are often usable in both an embedded or system-level context or in a full-fledged desktop GUI application.

Consider these common libraries you might use in either a `std` project (PC application, web microservice) or `no_std` project (embedded microcontroller firmware, bootloader, Linux kernel module, blockchain smart contract):

- data encoding (https://crates.io/crates/base64 for instance),

- hashing (SHA2 https://github.com/RustCrypto/hashes/tree/master/sha2),

- data structures (https://github.com/Lokathor/tinyvec)

- time/date manipulation (https://docs.rs/chrono/latest/chrono/)


As of recently, Jiff with time zone support can now be used in a no_std project. :-)

https://docs.rs/jiff/latest/jiff/tz/index.html#core-only-env...


Amazing, thanks! To anyone who hasn't tried it yet... Jiff is a fantastic time and date crate, I highly recommend you check it out, as a possible improvement over chrono and time.


I’ve used it in the past for creating a shared messaging crate that has packet definitions, handshake logic, serialization/deserialization.

I think with sloppy/complex code it could start to resemble #ifdef PLATFORM complexity if you do a lot inline, but cargo workspaces are a good way to reduce the blast radius.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: