Don't Worry, C and Rust Can FINALLY Coexist (Here's How) | Embedded Rust and C on the RP2040 Pi Pico

preview_player
Показать описание
FINALLY! Embedded C and Rust can coexist. For the longest time, there had been the great war of Rust vs C. In this video, I teach you how to create a Rust Crate and write C code for the Raspberry Pi Pico RP2040 that allows you to use both in an embedded environment. By creating a static archive library and linking it against your C code, you can compile Rust with your C for them to both run in the same runtime.

0:00 Introduction
1:17 Rust Library Crate
5:47 Linking Rust + C
7:43 Adding C to Rust
Рекомендации по теме
Комментарии
Автор

So when writing the Rust code, you used primitive Rust types. I know in the standard library there is the std::ffi C types that are redefined for Rust, but I understand here in the embedded space having the whole std library is overkill or not possible. My question is though, are there circumstances where the Rust types and C types don’t match in the function signature and the function either fails or causes undefined behavior? If so, would you happen to know any methods by which to avoid this behavior?
Thanks for the great video!

ShadowFluux
Автор

This is going to be very useful as I have a C project that I would love to add some Rust code into. Thank you for showing me the way.

TinkerersAnonymous
Автор

the edition in rust is actually the Rust major version, it basically the year it was released in, but its not the year you made your project in.

wChris_
Автор

Maybe to go further, you could create a library target in the CmakeLists file with perhaps a custom build command to build the whole binary all in one go with the use of a single make command.

thetos
Автор

This is extremely cool. Can't pretend to completely understand it, but the ability to use C libraries could be super-useful in future. Thank you!

chairmakerPete
Автор

you can actually pass the '--lib' flag to cargo new to make a library from the start

wChris_
Автор

Did you explain why you have the rust signatures for gpio_put_explicit and sleep_ms with return type i32, even though the actual C functions both return void?

meatmanek
Автор

This is excellent work. Clear, concise, thorough.

jaredkomoroski
Автор

I just wanted to thank you for your channel, and the wonderful work you do. Specially the rust embedded

zeroows
Автор

if you want rust and C to work together easier then make it compile to a obj file and link that to C meaning when compiling then no having to link a library just link both obj files

ltecheroffical
Автор

Well, that's convenient. I began a project with C-Rust interop a few hours ago.

gagagero
Автор

This is great, thanks! My current goals include learning C and Rust, so this video is perfect for me.

rebelmachine
Автор

9:33 I'm curious why you're "lying" to the compiler about the return value. Is it for demo purposes, are you unable to omit the return value, or are you unable to specify `-> ()`?

raffimolero
Автор

Really cool video! I was left with a couple of doubts,

In the video you declare functions that receive/return integers or booleans, which are variables that both C and Rust understand, what if you want to send a Rust reference or C pointer? Or a struct declared on C or Rust?

And is it possible to run C code alongside Rust using the embassy framework?

Juan-umdu
Автор

That's a cool demo, I'm interested in rust but I've never tried it before, it's an interesting use case !
Just une question, you "lied to the compiler" with the return values, why ?
Is it like in maths "when it's too complex for the moment, so we're gonna ignore it" ?

willexco
Автор

I maybe missed it, but when do you import the c library into rust? Or how does rust know where those two c functions are?

hansdampf
Автор

How do you validate that the ‘bool’ on the C side is the same bit-size as the ‘bool’ on the Rust side?
Also how do you validate that the ‘int’ on the C side is the same bit-size as the ‘i32’ on the Rust side?

IIRC, I remember that it may cause problems, ‘int’ in C may be 32 bits wide but it doesn’t have to, The same thing about the ‘bool’ type variable, Some languages use an 8 bit integer to represent it while others use a 32 bit integer to represent it, How do you validate that the types on both the C and Rust side align with each other? Thanks.

astroid-wspy
Автор

Can we use only rust without C for embedded development? Or is there a library which handles all of these FFI things?

InMemoryOfNeo
Автор

The "edition" key *does not mean* year created. Rust editions are are not released every year, and it defines a set of things you want that may contain breaking changes (such as the standard library). This allows those core components to be updated and have breaking changes without breaking old projects when they update rust, as they still use the old edition. The default edition is always the most recent edition, in this case 2021(however we may be getting edition 2024 later this year)

tryoxiss
Автор

Rust has just made it into the linux kernel. Proof that these two languages can live in peace

conor-smith
visit shbcf.ru