Difference between revisions of "Rust"

From Wiki at Neela Nurseries
Jump to navigation Jump to search
m (Add link to Rust Embedded Discovery-mb2 Github repository)
m (A Rust Embedded book by Ferrous Systems)
 
(8 intermediate revisions by the same user not shown)
Line 47: Line 47:
 
Books on Rust language for embedded systems, and a link to a specific topic regarding memory:
 
Books on Rust language for embedded systems, and a link to a specific topic regarding memory:
  
* https://docs.rust-embedded.org/book/
+
(1) https://docs.rust-embedded.org/book/
  
* https://docs.rust-embedded.org/discovery-mb2/
+
(2) https://docs.rust-embedded.org/discovery/microbit/07-uart/send-a-single-byte.html
  
* https://docs.rust-embedded.org/embedonomicon/memory-layout.html
+
(3) https://docs.rust-embedded.org/discovery-mb2/
  
Note as well that Rust Embedded Discovery-mb2 has its code repository at:
+
(4)  https://docs.rust-embedded.org/embedonomicon/memory-layout.html
 +
 
 +
Reference (2) and (3) are, or appear to be, first and second editions of the Rust book on embedded development with the Microbit dev board as the readers' example and target hardware.  Note as well that Rust Embedded Discovery-mb2 has its code repository at:
  
 
*  https://github.com/rust-embedded/discovery-mb2
 
*  https://github.com/rust-embedded/discovery-mb2
Line 62: Line 64:
  
 
*  https://crates.io/crates/stm32f4
 
*  https://crates.io/crates/stm32f4
 +
 +
Rust Embassy examples for Nordic Semi nrf:
 +
 +
*  https://docs.embassy.dev/embassy-nrf/git/nrf51/index.html
  
 
=== [[#top|^]] Specific development boards and Rust ===
 
=== [[#top|^]] Specific development boards and Rust ===
Line 77: Line 83:
 
*  https://github.com/charlesmuchene/minimal-rust-microbit-starter/tree/main
 
*  https://github.com/charlesmuchene/minimal-rust-microbit-starter/tree/main
  
=== Crate Microbit-V2 ===
+
Some kind of project template for Rust projects for Microbit-v2:
  
*  [[#top|^]] https://crates.io/crates/microbit-v2
+
https://github.com/br0kenpixel/microbit-rust-std/blob/main/Cargo.toml
 +
 
 +
=== [[#top|^]] Crate Microbit-V2 ===
 +
 
 +
https://crates.io/crates/microbit-v2
  
 
== [[#top|^]] Panic Handling ==
 
== [[#top|^]] Panic Handling ==
Line 95: Line 105:
 
<hr>
 
<hr>
  
== [[#top|^]] Rust General Topic ==
+
== [[#top|^]] Rust General Topics ==
  
 
Following sections touch on Rust language features, use and practice.  These notes are not specific to embedded development.
 
Following sections touch on Rust language features, use and practice.  These notes are not specific to embedded development.
Line 104: Line 114:
  
 
*  https://web.mit.edu/rust-lang_v1.25/arch/amd64_ubuntu1404/share/doc/rust/html/book/first-edition/crates-and-modules.html
 
*  https://web.mit.edu/rust-lang_v1.25/arch/amd64_ubuntu1404/share/doc/rust/html/book/first-edition/crates-and-modules.html
 +
 +
== [[#top|^]] Potentially Useful Crates ==
 +
 +
Rust Embassy crate to convey debug messages over USB . . .
 +
 +
*  https://crates.io/crates/defmt-embassy-usbserial
 +
 +
== [[#top|^]] Linker Flags ==
 +
 +
*  https://users.rust-lang.org/t/no-loadable-segments-were-found-in-the-elf-file-cargo-embed/117714/6
  
 
== - - - - -  - - - - -  - - - - -  - - - - -  - - - - -  - - - - -  - - - - -  - - - - -  - - - - -  - - - - - ==
 
== - - - - -  - - - - -  - - - - -  - - - - -  - - - - -  - - - - -  - - - - -  - - - - -  - - - - -  - - - - - ==
Line 135: Line 155:
 
#![no_main]
 
#![no_main]
 
</pre>
 
</pre>
 +
 +
== [[#top|^]] Things To Learn Well ==
 +
 +
Topics in Rust and programming in general to learn well.
 +
 +
Type-level programming in Rust . . .
 +
 +
*  https://users.rust-lang.org/t/microbit-roulette-example-code-organization/69630/2
 +
 +
Analysis and debugging tool rust-analyzer . . .
 +
 +
*  https://rust-analyzer.github.io/book/installation.html
 +
 +
<!--
 +
bradleyharden
 +
Dec 2021
 +
Just FYI, microbit::nrf52833_hal::Timer<microbit::nrf52833_pac::TIMER0> would normally be written as
 +
 +
use microbit::nrf52833_hal::Timer;
 +
use microbit::nrf52833_pac::TIMER0;
 +
 +
struct Application {
 +
    // ...
 +
    timer: Timer<TIMER0>,
 +
}
 +
The compiler isn't great at trimming the full path when displaying type names, but there has been work to improve that.
 +
 +
You should also note that somewhat complicated types are pretty common in the Rust embedded world. Debugging embedded software can be pretty tough, and things are often resource constrained. Consequently, people often make use of type-level programming techniques to encode invariants in the type system, so that they can be checked by the compiler. That turns run-time errors into compile-time errors, which can be a life saver.
 +
 +
Moreover, you can often make the type-level approach completely zero overhead. For example, GPIO Pin structs are often zero-sized, so they don't contain any actual data. Instead, the GPIO registers are written as side effects of type transformations in the source code. The result is the exact same thing you would hand write in C, but it's checked by the compiler for correctness.
 +
 +
There's definitely a little bit of a learning curve to type-level programming in Rust, but in my opinion, it's often worth it for embedded projects.
 +
-->
  
 
== [[#top|^]] References Rust and Embassy ==
 
== [[#top|^]] References Rust and Embassy ==
Line 143: Line 196:
  
 
*  https://doc.rust-lang.org/reference/inline-assembly.html
 
*  https://doc.rust-lang.org/reference/inline-assembly.html
 +
 +
What are `Some` and `None` in Rust . . .
 +
 +
*  https://stackoverflow.com/questions/24771655/what-are-some-and-none
 +
 +
A Rust Embedded book by Ferrous Systems . . .
 +
 +
*  https://rust-exercises.ferrous-systems.com/latest/book/nrf52-radio-running-from-vsc

Latest revision as of 00:46, 2 February 2026

Notes on Rust programming language

Overview

This local page about the programming language of Rust aims to answer among other questions: How suitable is Rust for embedded development?

Rust By Example (RBE) home page here:

MIT mirror of Rust introduction by chapters:

^ Organizations for Rust

^ Installation and Other Specific Notes

Install Rust on Ubuntu:

Adoption and popularity of Rust:

2025-12-16

Rust crates


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

^ Rust for Embedded Systems

Books on Rust language for embedded systems, and a link to a specific topic regarding memory:

(1) https://docs.rust-embedded.org/book/

(2) https://docs.rust-embedded.org/discovery/microbit/07-uart/send-a-single-byte.html

(3) https://docs.rust-embedded.org/discovery-mb2/

(4) https://docs.rust-embedded.org/embedonomicon/memory-layout.html

Reference (2) and (3) are, or appear to be, first and second editions of the Rust book on embedded development with the Microbit dev board as the readers' example and target hardware. Note as well that Rust Embedded Discovery-mb2 has its code repository at:

Rust crates for STM32:

Rust Embassy examples for Nordic Semi nrf:

^ Specific development boards and Rust

Rust Embassy project has board support crates for certain MCU families. Ted found a crate or sub-project named `stm32f0` which is adaptable to support STMicro's stm32f401re. Needed only to change the MCU name from something other than `stm32f401re` to this MCU name in the Cargo.toml file.

(Thought the Cargo.toml file referenced an STM part with "7" in the name but see https://github.com/embassy-rs/embassy/blob/main/examples/stm32f0/Cargo.toml which is similar to the starting point Ted found.)

Charles Muchene is a software developer working in Rust and other languages. He shares an article on Medium dot com and a github project which presents a minimal start-up program in Rust for the Microbit V2:

Some kind of project template for Rust projects for Microbit-v2:

^ Crate Microbit-V2

^ Panic Handling

When setting panic compilation to 'abort' on panic, may be necessary to write one's own panic function:

How Rust panic is implemented . . . in crates core and std:


^ Rust General Topics

Following sections touch on Rust language features, use and practice. These notes are not specific to embedded development.

^ Crates versus Modules

Excellent article at MIT explaining Rust crates and modules, and how to organize them:

^ Potentially Useful Crates

Rust Embassy crate to convey debug messages over USB . . .

^ Linker Flags

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

^ Questions to Answer

(1) For what is Rust language 'derive' keyword used?

  #[derive(Clone)]

  #[derive(Default)]

(2) How is the `.await` method working, and is this kind of element called a method in Rust?

(3) Rust `match` construct, is this equal to C switch statement?

(4) what does at or near top of source file `use defmt::*;` mean?

(5) Rust `impl` a keyword? Seeing this in lines like `impl CanTxChannel`.

(6) `.await.unwrap()` versus `.await`?

(7) What is the following forward arrow operator in this context?:

  pub async fn <function_name>(var declarations) -> () { ... }

(8) What mean the following top of file lines?:

#![no_std]
#![no_main]

^ Things To Learn Well

Topics in Rust and programming in general to learn well.

Type-level programming in Rust . . .

Analysis and debugging tool rust-analyzer . . .


^ References Rust and Embassy

What are `Some` and `None` in Rust . . .

A Rust Embedded book by Ferrous Systems . . .