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,20 @@
# 03 - Ownership and Borrowing (7 min)
## Goal
Ownership und Borrowing an einem kleinen Robot-State praktisch verstehen.
## Run
- `bash scripts/run-step.sh 03 task`
## Tasks
1. Lies die Funktionen mit `&RobotState` und `&mut RobotState`.
2. Ergänze die TODOs in der Task-Datei.
3. Achte auf Compiler-Fehler, wenn mutable/immutable borrows kollidieren.
## Done when
1. Das Programm kompiliert.
2. LED-State, Button-Count und ADC-Wert werden korrekt aktualisiert.

View File

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

View File

@@ -0,0 +1,41 @@
#[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) {
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);
}

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);
}