# Can Rust work on microcontrollers?

**URL:** <https://users.rust-lang.org/t/can-rust-work-on-microcontrollers/24314>\
**Category:** uncategorized\
**Created:** [January 18, 2019, 3:38am UTC](https://users.rust-lang.org/t/can-rust-work-on-microcontrollers/24314 "2019-01-18T03:38:33Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joe232](https://avatars.discourse-cdn.com/v4/letter/j/df705f/32.png) [@Joe232](https://users.rust-lang.org/u/Joe232)\
**Post date:** [January 18, 2019, 3:38am UTC](https://users.rust-lang.org/t/can-rust-work-on-microcontrollers/24314/1 "2019-01-18T03:38:33Z")

</div>

Can Rust work on microcontrollers just like C?

---

<div class="post-metadata">

**Author:** ![dcarosone](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/dcarosone/32/10360_2.png) [@dcarosone](https://users.rust-lang.org/u/dcarosone)\
**Post date:** [January 18, 2019, 3:49am UTC](https://users.rust-lang.org/t/can-rust-work-on-microcontrollers/24314/2 "2019-01-18T03:49:04Z")

</div>

Yes

> **[Embedded devices](https://www.rust-lang.org/what/embedded)**
>
> A language empowering everyone to build reliable and efficient software.

---

<div class="post-metadata">

**Author:** ![notriddle](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/notriddle/32/51307_2.png) [@notriddle](https://users.rust-lang.org/u/notriddle)\
**Post date:** [January 18, 2019, 3:59am UTC](https://users.rust-lang.org/t/can-rust-work-on-microcontrollers/24314/3 "2019-01-18T03:59:41Z")

</div>

There's no theoretical reason it can't.

Rust allows you to poke at arbitrary memory addresses with volatile loads and stores, just like C. It's considered `unsafe`, since Rust can't account for the effects of memory mapped I/O.

Rust compiles into straight assembly, with jumps, pointers, arithmetic instructions, a call stack, a data stack, and a heap store. Also just like C.

Rust allows you to compile without the filesystem or other OS facilities, which is probably something you want on a tiny microcontroller with no OS.

## But

Rust's compiler only supports CPU architectures that it has been made to support. It's been ported to the AVR, and made to run in the Arduino. It's been ported to a few other stuff, too. But it's not been ported to as many platforms as C.

---

<div class="post-metadata">

**Author:** ![Joe232](https://avatars.discourse-cdn.com/v4/letter/j/df705f/32.png) [@Joe232](https://users.rust-lang.org/u/Joe232)\
**Post date:** [January 18, 2019, 4:24am UTC](https://users.rust-lang.org/t/can-rust-work-on-microcontrollers/24314/4 "2019-01-18T04:24:50Z")

</div>

> [@notriddle](#):
>
> There’s no theoretical reason it can’t.
> 
> Rust allows you to poke at arbitrary memory addresses with volatile loads and stores, just like C. It’s considered `unsafe` , since Rust can’t account for the effects of memory mapped I/O.
> 
> Rust compiles into straight assembly, with jumps, pointers, arithmetic instructions, a call stack, a data stack, and a heap store. Also just like C.
> 
> Rust allows you to compile without the filesystem or other OS facilities, which is probably something you want on a tiny microcontroller with no OS.

So since we are using `unsafe` for microcontrollers, is there a benefit of using Rust over C if we are using it on a supported architecture?

---

<div class="post-metadata">

**Author:** ![CleanCut](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/cleancut/32/14794_2.png) [@CleanCut](https://users.rust-lang.org/u/CleanCut)\
**Post date:** [January 18, 2019, 4:47am UTC](https://users.rust-lang.org/t/can-rust-work-on-microcontrollers/24314/5 "2019-01-18T04:47:31Z")

</div>

> [@Joe232](#):
>
> So since we are using `unsafe` for microcontrollers, is there a benefit of using Rust over C if we are using it on a supported architecture?

Yes. Follow the link @dcarosone posted! The top section of that page answers that specific question.

---

<div class="post-metadata">

**Author:** ![Joe232](https://avatars.discourse-cdn.com/v4/letter/j/df705f/32.png) [@Joe232](https://users.rust-lang.org/u/Joe232)\
**Post date:** [January 18, 2019, 4:48am UTC](https://users.rust-lang.org/t/can-rust-work-on-microcontrollers/24314/6 "2019-01-18T04:48:15Z")

</div>

Oh thanks then.

---

<div class="post-metadata">

**Author:** ![TomP](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/tomp/32/4735_2.png) [@TomP](https://users.rust-lang.org/u/TomP)\
**Post date:** [January 18, 2019, 5:03am UTC](https://users.rust-lang.org/t/can-rust-work-on-microcontrollers/24314/7 "2019-01-18T05:03:33Z")

</div>

> [@Joe232](#):
>
> So since we are using `unsafe` for microcontrollers, is there a benefit of using Rust over C if we are using it on a supported architecture?

`unsafe` doesn't remove Rust's protections; instead it tells the compiler that for specific aspects of the code, the compiler should trust that the programmer has not broken a predefined, limited subset of those protections, rather than the compiler insisting that it be able to prove on its own that each of the relevant compiler requirements has not been broken.

---

<div class="post-metadata">

**Author:** ![farnbams](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/farnbams/32/12759_2.png) [@farnbams](https://users.rust-lang.org/u/farnbams)\
**Post date:** [January 18, 2019, 7:34am UTC](https://users.rust-lang.org/t/can-rust-work-on-microcontrollers/24314/8 "2019-01-18T07:34:00Z")

</div>

There has been a good talk about what benefits you gain from using Rust on a µC at last Rust Conf ([RustConf 2018 - Getting Something for Nothing by James Munns - YouTube](https://www.youtube.com/watch?v=t99L3JHhLc0&index=5&list=PL85XCvVPmGQi3tivxDDF1hrT9qr5hdMBZ)).

---

<div class="post-metadata">

**Author:** ![skade](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/skade/32/1127_2.png) [@skade](https://users.rust-lang.org/u/skade)\
**Post date:** [January 18, 2019, 10:49am UTC](https://users.rust-lang.org/t/can-rust-work-on-microcontrollers/24314/9 "2019-01-18T10:49:22Z")

</div>

> [@Joe232](#):
>
> So since we are using `unsafe` for microcontrollers, is there a benefit of using Rust over C if we are using it on a supported architecture?

Rust is using unsafe for arbitrary memory access on all platforms, libstd just abstracts it away from you.

---

<div class="post-metadata">

**Author:** ![dcarosone](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/dcarosone/32/10360_2.png) [@dcarosone](https://users.rust-lang.org/u/dcarosone)\
**Post date:** [January 18, 2019, 10:55am UTC](https://users.rust-lang.org/t/can-rust-work-on-microcontrollers/24314/10 "2019-01-18T10:55:40Z")

</div>

> [@skade](#):
>
> Rust is using unsafe for arbitrary memory access on all platforms, libstd just abstracts it away from you.

As does the `embedded-hal` (and similar) frameworks in the embedded ecosystem. I've not written a single `unsafe` anywhere in an embedded project for stm32.

---

<div class="post-metadata">

**Author:** ![johnthagen](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/johnthagen/32/3318_2.png) [@johnthagen](https://users.rust-lang.org/u/johnthagen)\
**Post date:** [January 18, 2019, 2:35pm UTC](https://users.rust-lang.org/t/can-rust-work-on-microcontrollers/24314/11 "2019-01-18T14:35:48Z")

</div>

@japaric's [Brave New I/O](https://blog.japaric.io/brave-new-io/) is a great read to show how Rust's owernship model can be used to protect more than memory safety - also resource/IO usage.

---

<div class="post-metadata">

**Author:** ![Joe232](https://avatars.discourse-cdn.com/v4/letter/j/df705f/32.png) [@Joe232](https://users.rust-lang.org/u/Joe232)\
**Post date:** [January 30, 2019, 7:13am UTC](https://users.rust-lang.org/t/can-rust-work-on-microcontrollers/24314/12 "2019-01-30T07:13:05Z")

</div>

> [@notriddle](#):
>
> and made to run in the Arduino

One question, is an Arduino a microcontroller?

---

<div class="post-metadata">

**Author:** ![FilipeMazzon](https://avatars.discourse-cdn.com/v4/letter/f/a698b9/32.png) [@FilipeMazzon](https://users.rust-lang.org/u/FilipeMazzon)\
**Post date:** [April 22, 2019, 6:51pm UTC](https://users.rust-lang.org/t/can-rust-work-on-microcontrollers/24314/13 "2019-04-22T18:51:08Z")

</div>

Yes, it is, but is recommend for only education, because you can t sell a product with the arduino

---

<div class="post-metadata">

**Author:** ![robertpiosik](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/robertpiosik/32/12022_2.png) [@robertpiosik](https://users.rust-lang.org/u/robertpiosik)\
**Post date:** [July 25, 2019, 8:46am UTC](https://users.rust-lang.org/t/can-rust-work-on-microcontrollers/24314/14 "2019-07-25T08:46:39Z")

</div>

That's incorrect statement. Read more here: [https://www.arduino.cc/en/Main/FAQ#toc10](https://www.arduino.cc/en/Main/FAQ#toc10)

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/steffahn/32/47569_2.png) [@steffahn](https://users.rust-lang.org/u/steffahn)\
**Post date:** [July 3, 2022, 2:45pm UTC](https://users.rust-lang.org/t/can-rust-work-on-microcontrollers/24314/15 "2022-07-03T14:45:35Z")

</div>

Closed on July 3, 2022, 2:45pm UTC


