Skip to content

Commit 8233599

Browse files
authored
Use clamp in bare-metal compass solution (#2537)
The custom function `cap` does the same as `Ord::clamp`, which was introduced in Rust 1.50. Let's use the latter instead. I've flashed the new program onto my microbit and can confirm it still works as intended.
1 parent 76e2cfe commit 8233599

File tree

1 file changed

+2
-6
lines changed
  • src/exercises/bare-metal/compass/src

1 file changed

+2
-6
lines changed

src/exercises/bare-metal/compass/src/main.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ extern crate panic_halt as _;
2222
use core::fmt::Write;
2323
use cortex_m_rt::entry;
2424
// ANCHOR_END: top
25-
use core::cmp::{max, min};
2625
use embedded_hal::digital::InputPin;
2726
use lsm303agr::{
2827
AccelMode, AccelOutputDataRate, Lsm303agr, MagMode, MagOutputDataRate,
@@ -163,9 +162,6 @@ impl Mode {
163162
fn scale(value: i32, min_in: i32, max_in: i32, min_out: i32, max_out: i32) -> i32 {
164163
let range_in = max_in - min_in;
165164
let range_out = max_out - min_out;
166-
cap(min_out + range_out * (value - min_in) / range_in, min_out, max_out)
167-
}
168-
169-
fn cap(value: i32, min_value: i32, max_value: i32) -> i32 {
170-
max(min_value, min(value, max_value))
165+
let scaled = min_out + range_out * (value - min_in) / range_in;
166+
scaled.clamp(min_out, max_out)
171167
}

0 commit comments

Comments
 (0)