Skip to content

Commit 0a03b55

Browse files
committed
examples: avoid format collect
Fixes: ``` error: use of `format!` to build up a string from an iterator --> rcgen/examples/rsa-irc.rs:30:25 | 30 | let hash_hex: String = hash.as_ref().iter().map(|b| format!("{:02x}", b)).collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | help: call `fold` instead --> rcgen/examples/rsa-irc.rs:30:46 | 30 | let hash_hex: String = hash.as_ref().iter().map(|b| format!("{:02x}", b)).collect(); | ^^^ help: ... and use the `write!` macro here --> rcgen/examples/rsa-irc.rs:30:54 | 30 | let hash_hex: String = hash.as_ref().iter().map(|b| format!("{:02x}", b)).collect(); | ^^^^^^^^^^^^^^^^^^^^ = note: this can be written more efficiently by appending to a `String` directly = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#format_collect = note: `-D clippy::format-collect` implied by `-D warnings` ```
1 parent 22f38c5 commit 0a03b55

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

rcgen/examples/rsa-irc-openssl.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
fn main() -> Result<(), Box<dyn std::error::Error>> {
44
use rcgen::{date_time_ymd, Certificate, CertificateParams, DistinguishedName};
5+
use std::fmt::Write;
56
use std::fs;
67

78
let mut params: CertificateParams = Default::default();
@@ -21,7 +22,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
2122
let pem = pem::parse(&pem_serialized)?;
2223
let der_serialized = pem.contents();
2324
let hash = ring::digest::digest(&ring::digest::SHA512, der_serialized);
24-
let hash_hex: String = hash.as_ref().iter().map(|b| format!("{b:02x}")).collect();
25+
let hash_hex = hash.as_ref().iter().fold(String::new(), |mut output, b| {
26+
let _ = write!(output, "{b:02x}");
27+
output
28+
});
2529
println!("sha-512 fingerprint: {hash_hex}");
2630
println!("{pem_serialized}");
2731
println!("{}", cert.serialize_private_key_pem());

rcgen/examples/rsa-irc.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
66
use rsa::RsaPrivateKey;
77

88
use rcgen::{date_time_ymd, Certificate, CertificateParams, DistinguishedName};
9+
use std::fmt::Write;
910
use std::fs;
1011

1112
let mut params: CertificateParams = Default::default();
@@ -27,7 +28,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
2728
let pem = pem::parse(&pem_serialized)?;
2829
let der_serialized = pem.contents();
2930
let hash = ring::digest::digest(&ring::digest::SHA512, der_serialized);
30-
let hash_hex: String = hash.as_ref().iter().map(|b| format!("{:02x}", b)).collect();
31+
let hash_hex = hash.as_ref().iter().fold(String::new(), |mut output, b| {
32+
let _ = write!(output, "{b:02x}");
33+
output
34+
});
3135
println!("sha-512 fingerprint: {hash_hex}");
3236
println!("{pem_serialized}");
3337
println!("{}", cert.serialize_private_key_pem());

0 commit comments

Comments
 (0)