Skip to content

Commit d77b1c2

Browse files
senekorpetertseng
authored andcommitted
exercises/*: inline variables in format! strings
https://rust-lang.github.io/rust-clippy/master/index.html#uninlined_format_args Non-inlined code is slightly more difficult to read and understand, as it requires arguments to be matched against the format string. The inlined syntax, where allowed, is simpler.
1 parent 6a12a29 commit d77b1c2

File tree

14 files changed

+29
-53
lines changed

14 files changed

+29
-53
lines changed

exercises/concept/resistor-color/.meta/exemplar.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub fn color_to_value(color: ResistorColor) -> usize {
2222

2323
pub fn value_to_color_string(value: usize) -> String {
2424
ResistorColor::from_int(value)
25-
.map(|color| format!("{:?}", color))
25+
.map(|color| format!("{color:?}"))
2626
.unwrap_or_else(|_| "value out of range".into())
2727
}
2828

exercises/concept/semi-structured-logs/.meta/exemplar.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ pub fn log(level: LogLevel, message: &str) -> String {
1010
LogLevel::Info => info(message),
1111
LogLevel::Warning => warn(message),
1212
LogLevel::Error => error(message),
13-
LogLevel::Debug => format!("[DEBUG]: {}", message),
13+
LogLevel::Debug => format!("[DEBUG]: {message}"),
1414
}
1515
}
1616
pub fn info(message: &str) -> String {
17-
format!("[INFO]: {}", message)
17+
format!("[INFO]: {message}")
1818
}
1919
pub fn warn(message: &str) -> String {
20-
format!("[WARNING]: {}", message)
20+
format!("[WARNING]: {message}")
2121
}
2222
pub fn error(message: &str) -> String {
23-
format!("[ERROR]: {}", message)
23+
format!("[ERROR]: {message}")
2424
}

exercises/practice/affine-cipher/tests/affine-cipher.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,9 @@ fn encode_with_a_not_coprime_to_m() {
6262
const EXPECTED_ERROR: AffineCipherError = AffineCipherError::NotCoprime(6);
6363
match encode("This is a test.", 6, 17) {
6464
Err(EXPECTED_ERROR) => (),
65-
Err(err) => panic!(
66-
"Incorrect error: expected: {:?}, actual: {:?}.",
67-
EXPECTED_ERROR, err
68-
),
65+
Err(err) => panic!("Incorrect error: expected: {EXPECTED_ERROR:?}, actual: {err:?}."),
6966
Ok(r) => panic!(
70-
"Cannot encode/decode when a is coprime to m: expected: {:?}, actual: {:?}.",
71-
EXPECTED_ERROR, r
67+
"Cannot encode/decode when a is coprime to m: expected: {EXPECTED_ERROR:?}, actual: {r:?}."
7268
),
7369
}
7470
}
@@ -134,13 +130,9 @@ fn decode_with_a_not_coprime_to_m() {
134130
const EXPECTED_ERROR: AffineCipherError = AffineCipherError::NotCoprime(13);
135131
match decode("Test", 13, 11) {
136132
Err(EXPECTED_ERROR) => (),
137-
Err(e) => panic!(
138-
"Incorrect error: expected: {:?}, actual: {:?}.",
139-
EXPECTED_ERROR, e
140-
),
133+
Err(e) => panic!("Incorrect error: expected: {EXPECTED_ERROR:?}, actual: {e:?}."),
141134
Ok(r) => panic!(
142-
"Cannot encode/decode when a is coprime to m: expected: {:?}, actual: {:?}.",
143-
EXPECTED_ERROR, r
135+
"Cannot encode/decode when a is coprime to m: expected: {EXPECTED_ERROR:?}, actual: {r:?}."
144136
),
145137
}
146138
}

exercises/practice/allergies/tests/allergies.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,13 @@ use allergies::*;
33
fn compare_allergy_vectors(expected: &[Allergen], actual: &[Allergen]) {
44
for element in expected {
55
if !actual.contains(element) {
6-
panic!(
7-
"Allergen missing\n {:?} should be in {:?}",
8-
element, actual
9-
);
6+
panic!("Allergen missing\n {element:?} should be in {actual:?}");
107
}
118
}
129

1310
if actual.len() != expected.len() {
1411
panic!(
15-
"Allergy vectors are of different lengths\n expected {:?}\n got {:?}",
16-
expected, actual
12+
"Allergy vectors are of different lengths\n expected {expected:?}\n got {actual:?}"
1713
);
1814
}
1915
}

exercises/practice/circular-buffer/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ impl<T> CircularBuffer<T> {
1616
"Construct a new CircularBuffer with the capacity to hold {}.",
1717
match capacity {
1818
1 => "1 element".to_string(),
19-
_ => format!("{} elements", capacity),
19+
_ => format!("{capacity} elements"),
2020
}
2121
);
2222
}

exercises/practice/dominoes/tests/dominoes.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,15 @@ fn check(input: &[Domino]) -> CheckResult {
6767
fn assert_correct(input: &[Domino]) {
6868
match check(input) {
6969
Correct => (),
70-
GotInvalid => panic!("Unexpectedly got invalid on input {:?}", input),
71-
ChainingFailure(output) => panic!(
72-
"Chaining failure for input {:?}, output {:?}",
73-
input, output
74-
),
70+
GotInvalid => panic!("Unexpectedly got invalid on input {input:?}"),
71+
ChainingFailure(output) => {
72+
panic!("Chaining failure for input {input:?}, output {output:?}")
73+
}
7574
LengthMismatch(output) => {
76-
panic!("Length mismatch for input {:?}, output {:?}", input, output)
75+
panic!("Length mismatch for input {input:?}, output {output:?}")
7776
}
7877
DominoMismatch(output) => {
79-
panic!("Domino mismatch for input {:?}, output {:?}", input, output)
78+
panic!("Domino mismatch for input {input:?}, output {output:?}")
8079
}
8180
}
8281
}

exercises/practice/fizzy/tests/fizzy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ fn test_minimal_generic_bounds() {
9292
impl fmt::Display for Fizzable {
9393
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
9494
let Fizzable(ref n) = self;
95-
write!(f, "{}", n)
95+
write!(f, "{n}")
9696
}
9797
}
9898

exercises/practice/grep/tests/grep.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ fn set_up_files(files: &[(&str, &str)]) {
8787
for (file_name, file_content) in files {
8888
fs::write(file_name, file_content).unwrap_or_else(|_| {
8989
panic!(
90-
"Error setting up file '{}' with the following content:\n{}",
91-
file_name, file_content
90+
"Error setting up file '{file_name}' with the following content:\n{file_content}"
9291
)
9392
});
9493
}
@@ -97,7 +96,7 @@ fn set_up_files(files: &[(&str, &str)]) {
9796
fn tear_down_files(files: &[&str]) {
9897
for file_name in files {
9998
fs::remove_file(file_name)
100-
.unwrap_or_else(|_| panic!("Could not delete file '{}'", file_name));
99+
.unwrap_or_else(|_| panic!("Could not delete file '{file_name}'"));
101100
}
102101
}
103102

exercises/practice/leap/tests/leap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,6 @@ fn test_years_1600_to_1699() {
9595
.collect::<Vec<_>>();
9696

9797
if !incorrect_years.is_empty() {
98-
panic!("incorrect result for years: {:?}", incorrect_years);
98+
panic!("incorrect result for years: {incorrect_years:?}");
9999
}
100100
}

exercises/practice/macros/tests/macros.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,7 @@ mod simple_trybuild {
207207
if let Ok(result) = result {
208208
assert!(
209209
!result.status.success(),
210-
"Expected {:?} to fail to compile, but it succeeded.",
211-
file_path
210+
"Expected {file_path:?} to fail to compile, but it succeeded."
212211
);
213212
} else {
214213
panic!("Running subprocess failed.");

0 commit comments

Comments
 (0)