Would it be better to code c from rust

Is it similar to using libc and disabling Rust's standard library? I know it's like coding in C but with extra steps. Could I also access parts of the C++ standard library? The use case is to compile my program on a system that only has libc ported, or to create a slightly more lightweight or performant executable.

Also would I import headers or use libraries, even if it's a DLL or source code, similar to the 'use' keyword? I've heard of bindgen, of course.

It is not possible to remove the entire std library because the Rust language is bound tightly to many parts of its std library. The language depends on some of the traits, for example.

However, people do sometimes use the core packages without using the other packages in std. This is not because it would be "more lightweight or performant", it is because they're writing embedded software where there may be no disk, memory heap, etc. Or because they are implementing an OS.

See the FFI doc for interfacing to C (C++ is not supported, or not yet at least).

If you're looking for a way to reduce the executable size, it's worth looking up resources like GitHub - johnthagen/min-sized-rust: 🦀 How to minimize Rust binary size 📦 for options and tools. It mentions disabling std and only using core, but there's a bunch of other options to try first, that are less limiting.

1 Like

Yep, this is totally possible. The Rust std isn't terribly special, it just calls into libc and builds safe abstractions on top of it.

You can use the #![no_std] attribute at the top of your crate to opt out of std and just use core or alloc.

I swear I remember reading about someone that made a Windows GUI program using only the winapi (the DLL, not the rust wrapper crate) and core.

2 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.