Std VS no_std + alloc + libc

When we are writing a Rust library, what is the difference between normal std mode with no_std + alloc + libc?

I'm not so familiar with bare metal programming, but as far as I understand, for a Rust library, the main difference between core and std is platform-specific functionalities. So if we link to libc, are there any situations where the library works with no_std + alloc + libc, but not with std? In other word, if we are using no_std + alloc + libc in a Rust library, is it better just switching to std?

So if we link to libc, are there any situations where the library works with no_std + alloc + libc, but not with std?

If by “libc” you mean the platform C standard library and not the Rust library crate libc, then: yes, this could work on platforms where Rust std has not yet been ported.

You get the safety of std's safe wrappers for platform functionality. And possibly easier compatibility with platforms that don’t use C or POSIX standards but I don’t know if there are any of those. (I should really look into how WASI works some day.)

Also if you have a bin crate, using libstd will do a couple of useful things for you:

  • Registering a SIGSEGV handler which detects stack overflow and reports a nicer error.
  • Ignoring SIGPIPE so writing to a closed pipe returns an error rather than aborting your process.
  • Ensuring stdin/stdout/stderr are always open (opening them as /dev/null as necessary) to avoid IO safety issues.
3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.