start tutorial
This commit is contained in:
20
tutorial/03-rust-ownership-borrow/README.md
Normal file
20
tutorial/03-rust-ownership-borrow/README.md
Normal 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.
|
||||
4
tutorial/03-rust-ownership-borrow/solution/Cargo.toml
Normal file
4
tutorial/03-rust-ownership-borrow/solution/Cargo.toml
Normal file
@@ -0,0 +1,4 @@
|
||||
[package]
|
||||
name = "step03_rust_ownership_borrow_solution"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
41
tutorial/03-rust-ownership-borrow/solution/src/main.rs
Normal file
41
tutorial/03-rust-ownership-borrow/solution/src/main.rs
Normal 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);
|
||||
}
|
||||
4
tutorial/03-rust-ownership-borrow/task/Cargo.toml
Normal file
4
tutorial/03-rust-ownership-borrow/task/Cargo.toml
Normal file
@@ -0,0 +1,4 @@
|
||||
[package]
|
||||
name = "step03_rust_ownership_borrow_task"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
42
tutorial/03-rust-ownership-borrow/task/src/main.rs
Normal file
42
tutorial/03-rust-ownership-borrow/task/src/main.rs
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user