Skip to content

Commit cca43b5

Browse files
authored
Merge pull request #49 from hnez/cargo-clippy-leds
led: extras: fix cargo clippy complaining about String construction
2 parents 3cb20ac + 654f62e commit cca43b5

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/led/extras.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// with this program; if not, write to the Free Software Foundation, Inc.,
1616
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1717

18+
use std::fmt::Write;
1819
use std::io::Result;
1920
use std::time::Duration;
2021

@@ -134,15 +135,19 @@ impl Pattern for Leds {
134135
fn set_pattern(&self, pattern: BlinkPattern) -> Result<()> {
135136
let max = self.max_brightness()? as f32;
136137
let repetitions = pattern.repetitions;
137-
let pattern: String = pattern
138-
.steps
139-
.iter()
140-
.map(|(brightness, duration)| {
141-
let brightness = (brightness * max).round();
142-
let duration = duration.as_millis();
143-
format!("{} {} ", brightness, duration)
144-
})
145-
.collect();
138+
let pattern =
139+
pattern
140+
.steps
141+
.iter()
142+
.fold(String::new(), |mut dst, (brightness, duration)| {
143+
let brightness = (brightness * max).round();
144+
let duration = duration.as_millis();
145+
146+
write!(dst, "{} {} ", brightness, duration)
147+
.expect("Writing to a String should never fail");
148+
149+
dst
150+
});
146151

147152
self.write_file("trigger", "pattern")?;
148153
self.write_file("pattern", pattern)?;

0 commit comments

Comments
 (0)