Skip to content

Commit 0c60cb8

Browse files
Merge pull request #3 from flosse/use-iterator
use iterators to build attrs/style string
2 parents e0719fe + 6c9a04e commit 0c60cb8

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

src/dom_types.rs

+9-13
Original file line numberDiff line numberDiff line change
@@ -303,11 +303,10 @@ impl Attrs {
303303
}
304304

305305
pub fn as_str(&self) -> String {
306-
let mut result = String::new();
307-
for (key, val) in &self.vals {
308-
result += &format!(" {k}=\"{v}\"", k=key, v=val);
309-
}
310-
result
306+
self.vals.iter()
307+
.map(|(k,v)|format!("{}=\"{}\"", k, v))
308+
.collect::<Vec<_>>()
309+
.join(" ")
311310
}
312311

313312
pub fn add(&mut self, key: &str, val: &str) {
@@ -346,14 +345,11 @@ impl Style {
346345
/// Output style as a string, as would be set in the DOM as the attribute value
347346
/// for 'style'. Eg: "display: flex; font-size: 1.5em"
348347
pub fn as_str(&self) -> String {
349-
let mut result = String::new();
350-
if self.vals.keys().len() > 0 {
351-
for (key, val) in &self.vals {
352-
result += &format!("{k}: {v}; ", k = key, v = val);
353-
}
354-
}
355-
356-
result
348+
self.vals
349+
.iter()
350+
.map(|(k,v)|format!("{}:{};",k,v))
351+
.collect::<Vec<_>>()
352+
.join(";")
357353
}
358354

359355
pub fn add(&mut self, key: &str, val: &str) {

0 commit comments

Comments
 (0)