refactor for streamlining
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
[package]
|
||||
name = "step03_rust_ownership_borrow_task"
|
||||
name = "rust_ownership_borrow_task"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
@@ -1,20 +1,3 @@
|
||||
# 03 - Ownership and Borrowing (7 min)
|
||||
|
||||
## Goal
|
||||
# 03 - Ownership and Borrowing
|
||||
|
||||
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.
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
[package]
|
||||
name = "step03_rust_ownership_borrow_solution"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
@@ -6,27 +6,31 @@ struct RobotState {
|
||||
}
|
||||
|
||||
fn print_state(state: &RobotState) {
|
||||
// TODO: print the different elements of RobotState
|
||||
println!(
|
||||
"state => led_on={}, button_count={}, last_adc={}",
|
||||
state.led_on, state.button_count, state.last_adc
|
||||
"state => led_on={}, button_count={}, last_adc={}",
|
||||
???
|
||||
);
|
||||
}
|
||||
|
||||
fn toggle_led(state: &mut RobotState) {
|
||||
state.led_on = !state.led_on;
|
||||
// TODO: change the LED state to the inverse of itself
|
||||
state.led_on = ??
|
||||
}
|
||||
|
||||
fn record_button_press(state: &mut RobotState) {
|
||||
state.button_count += 1;
|
||||
// TODO: increase the button counter on press
|
||||
state.button_count = ??
|
||||
}
|
||||
|
||||
fn update_adc(state: &mut RobotState, raw: u16) {
|
||||
// TODO: keep this function as the single writer for ADC state.
|
||||
state.last_adc = raw;
|
||||
// TODO: update the adc value with the given one
|
||||
state.last_adc = ??
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut state = RobotState {
|
||||
// TODO: change the RobotState so we can edit its values
|
||||
let state = RobotState {
|
||||
led_on: false,
|
||||
button_count: 0,
|
||||
last_adc: 0,
|
||||
Reference in New Issue
Block a user