My original approach:
pub fn find_the_difference(s: String, t: String) -> char {
let mut c:u8 = 0;
for i in 0..s.chars().count() {
c ^= t.chars().nth(i).unwrap() as u8 ^ s.chars().nth(i).unwrap() as u8;
}
return (c ^ t.chars().nth(s.chars().count()).unwrap() as u8) as char;
}
But it was too slow and quite crazy also all what I had to write to replace t[i] ^ s[i]
(see below original C++ function). So I looked for something else and found this method where we convert the string into a char array and got some good results (went from 8ms to 0ms).
pub fn find_the_difference(s1: String, t1: String) -> char {
let mut c:u8 = 0;
let s: Vec<char> = s1.chars().collect();
let t: Vec<char> = t1.chars().collect();
for i in 0..s1.chars().count() {
c ^= t[i] as u8 ^ s[i] as u8;
}
return (c ^ t[s1.chars().count()] as u8) as char;
}
But perhaps no need to collect, nor do I care about index, I just want to iterate on one char after another. My current attempt:
pub fn find_the_difference(s1: String, t1: String) -> char {
let mut c:u8 = 0;
let mut s = s1.chars();
let mut t = t1.chars();
let n = s.count();
for i in 0..n {
c ^= t.next().unwrap() as u8 ^ s.next().unwrap() as u8; // c ^= *t++ ^ *s++ translated in C++
}
return (c ^ t.next().unwrap() as u8) as char;
}
I get the following error message:
Line 9, Char 44: borrow of moved value: `s` (solution.rs)
|
4 | let mut s = s1.chars();
| ----- move occurs because `s` has type `std::str::Chars<'_>`, which does not implement the `Copy` trait
5 | let mut t = t1.chars();
6 | let n = s.count();
| - value moved here
...
9 | c ^= t.next().unwrap() as u8 ^ s.next().unwrap() as u8;
| ^ value borrowed here after move
error: aborting due to previous error
Is it possible to achieve this kind of code c = *t++
?
NB: s1.chars.count() = t1.chars.count() - 1 and the goal is to find the extra letter in t1
NB2: original C++ function:
char findTheDifference(string s, string t) {
char c = 0;
for (int i = 0; t[i]; i++)
c ^= t[i] ^ s[i];
return c;
}