start tutorial

This commit is contained in:
2026-03-08 19:41:38 +01:00
commit a48ba2963d
81 changed files with 1738 additions and 0 deletions

View File

@@ -0,0 +1,4 @@
[package]
name = "step03_rust_ownership_borrow_task"
version = "0.1.0"
edition = "2021"

View File

@@ -0,0 +1,42 @@
#[derive(Debug)]
struct RobotState {
led_on: bool,
button_count: u32,
last_adc: u16,
}
fn print_state(state: &RobotState) {
println!(
"state => led_on={}, button_count={}, last_adc={}",
state.led_on, state.button_count, state.last_adc
);
}
fn toggle_led(state: &mut RobotState) {
state.led_on = !state.led_on;
}
fn record_button_press(state: &mut RobotState) {
state.button_count += 1;
}
fn update_adc(state: &mut RobotState, raw: u16) {
// TODO: keep this function as the single writer for ADC state.
state.last_adc = raw;
}
fn main() {
let mut state = RobotState {
led_on: false,
button_count: 0,
last_adc: 0,
};
print_state(&state);
toggle_led(&mut state);
record_button_press(&mut state);
update_adc(&mut state, 2024);
print_state(&state);
}