diff --git a/solution/2700-2799/2785.Sort Vowels in a String/README.md b/solution/2700-2799/2785.Sort Vowels in a String/README.md index fac01daca9603..12243933971a5 100644 --- a/solution/2700-2799/2785.Sort Vowels in a String/README.md +++ b/solution/2700-2799/2785.Sort Vowels in a String/README.md @@ -182,6 +182,33 @@ function sortVowels(s: string): string { } ``` +#### Rust + +```rust +impl Solution { + pub fn sort_vowels(s: String) -> String { + fn is_vowel(c: char) -> bool { + matches!(c.to_ascii_lowercase(), 'a' | 'e' | 'i' | 'o' | 'u') + } + + let mut vs: Vec = s.chars().filter(|&c| is_vowel(c)).collect(); + vs.sort_unstable(); + + let mut cs: Vec = s.chars().collect(); + let mut j = 0; + + for (i, c) in cs.clone().into_iter().enumerate() { + if is_vowel(c) { + cs[i] = vs[j]; + j += 1; + } + } + + cs.into_iter().collect() + } +} +``` + #### C# ```cs diff --git a/solution/2700-2799/2785.Sort Vowels in a String/README_EN.md b/solution/2700-2799/2785.Sort Vowels in a String/README_EN.md index 6356ec69d343e..f21d4201dc5f1 100644 --- a/solution/2700-2799/2785.Sort Vowels in a String/README_EN.md +++ b/solution/2700-2799/2785.Sort Vowels in a String/README_EN.md @@ -180,6 +180,33 @@ function sortVowels(s: string): string { } ``` +#### Rust + +```rust +impl Solution { + pub fn sort_vowels(s: String) -> String { + fn is_vowel(c: char) -> bool { + matches!(c.to_ascii_lowercase(), 'a' | 'e' | 'i' | 'o' | 'u') + } + + let mut vs: Vec = s.chars().filter(|&c| is_vowel(c)).collect(); + vs.sort_unstable(); + + let mut cs: Vec = s.chars().collect(); + let mut j = 0; + + for (i, c) in cs.clone().into_iter().enumerate() { + if is_vowel(c) { + cs[i] = vs[j]; + j += 1; + } + } + + cs.into_iter().collect() + } +} +``` + #### C# ```cs diff --git a/solution/2700-2799/2785.Sort Vowels in a String/Solution.rs b/solution/2700-2799/2785.Sort Vowels in a String/Solution.rs new file mode 100644 index 0000000000000..90c51f500d3f0 --- /dev/null +++ b/solution/2700-2799/2785.Sort Vowels in a String/Solution.rs @@ -0,0 +1,22 @@ +impl Solution { + pub fn sort_vowels(s: String) -> String { + fn is_vowel(c: char) -> bool { + matches!(c.to_ascii_lowercase(), 'a' | 'e' | 'i' | 'o' | 'u') + } + + let mut vs: Vec = s.chars().filter(|&c| is_vowel(c)).collect(); + vs.sort_unstable(); + + let mut cs: Vec = s.chars().collect(); + let mut j = 0; + + for (i, c) in cs.clone().into_iter().enumerate() { + if is_vowel(c) { + cs[i] = vs[j]; + j += 1; + } + } + + cs.into_iter().collect() + } +}