From ea43830954eb64b0944a8ac74d472a2e4dced8a1 Mon Sep 17 00:00:00 2001 From: Bauke Date: Fri, 8 Apr 2022 23:00:38 +0200 Subject: [PATCH] Make changes suggested by Clippy. --- source/add_binary/mod.rs | 5 ++--- source/longest_common_prefix/mod.rs | 5 ++--- source/plus_one/mod.rs | 2 +- source/valid_anagram/mod.rs | 4 ++-- source/valid_parenthesis/mod.rs | 2 +- 5 files changed, 8 insertions(+), 10 deletions(-) diff --git a/source/add_binary/mod.rs b/source/add_binary/mod.rs index 26184aa..2f14a79 100644 --- a/source/add_binary/mod.rs +++ b/source/add_binary/mod.rs @@ -9,9 +9,8 @@ pub fn add_binary(a: String, b: String) -> String { let mut sum = String::new(); // Create reversed iterators for both binary strings. - let (mut a, mut b) = (a.chars().rev(), b.chars().rev()); - - while let Some(a) = a.next() { + let mut b = b.chars().rev(); + for a in a.chars().rev() { // Safe to unwrap since we know both iterators will be the same length. let b = b.next().unwrap(); diff --git a/source/longest_common_prefix/mod.rs b/source/longest_common_prefix/mod.rs index f09373d..c98f687 100644 --- a/source/longest_common_prefix/mod.rs +++ b/source/longest_common_prefix/mod.rs @@ -1,7 +1,6 @@ -pub fn longest_common_prefix(strings: Vec) -> String { +pub fn longest_common_prefix(mut strings: Vec) -> String { // Sort the strings ascending by length. - let mut strings = strings.clone(); - strings.sort_by(|a, b| a.len().cmp(&b.len())); + strings.sort_by_key(|a| a.len()); // Then grab the shortest string. let shortest_string = match strings.first() { diff --git a/source/plus_one/mod.rs b/source/plus_one/mod.rs index a42fe1c..749818d 100644 --- a/source/plus_one/mod.rs +++ b/source/plus_one/mod.rs @@ -8,7 +8,7 @@ pub fn plus_one(digits: Vec) -> Vec { // Create a vector to store the incremented digits. let mut incremented = vec![]; - while let Some(digit) = digits.next() { + for digit in digits.by_ref() { if digit == 9 { // When the current digit is 9 save 0 and keep going through the digits. incremented.push(0); diff --git a/source/valid_anagram/mod.rs b/source/valid_anagram/mod.rs index 2eba4e1..f2aa0ba 100644 --- a/source/valid_anagram/mod.rs +++ b/source/valid_anagram/mod.rs @@ -6,10 +6,10 @@ pub fn is_anagram(a: String, b: String) -> bool { // Sort both strings' characters so they're in alphabetical order. let mut a = a.chars().collect::>(); - a.sort(); + a.sort_unstable(); let mut b = b.chars().collect::>(); - b.sort(); + b.sort_unstable(); // And if they are equal, then they're anagrams. a == b diff --git a/source/valid_parenthesis/mod.rs b/source/valid_parenthesis/mod.rs index e351ac2..522263e 100644 --- a/source/valid_parenthesis/mod.rs +++ b/source/valid_parenthesis/mod.rs @@ -35,5 +35,5 @@ pub fn is_valid(string: String) -> bool { // After the string has been looped over, if anything is left in the stack the // string is invalid. - stack.len() == 0 + stack.is_empty() }