Assuming that replacement with X is per character.
X
fn same(bad: &str, real: &str) -> bool { bad.len() == real.len() && bad .chars() .zip(real.chars()) .all(|(b, r)| b == r || (b == 'X' && r != '\n')) }
yay, a oneliner
#![feature(iter_order_by)] fn same(bad: &str, real: &str) -> bool { bad.len() == real.len() && bad .chars() .eq_by(real.chars(), |b, r| b == r || (b == 'X' && r != '\n')) }
all
fn same(bad: &str, real: &str) -> bool { if bad.len() != real.len() { return false; } for (b, r) in bad.chars().zip(real.chars()) { if b != r && (b != 'X' || r == '\n') { return false; } } true }
.zip()
fn same(bad: &str, real: &str) -> bool { if bad.len() != real.len() { return false; } let mut bad = bad.chars(); let mut real = real.chars(); while let (Some(b), Some(r)) = (bad.next(), real.next()) { if b != r && (b != 'X' || r == '\n') { return false; } } true }
(none of the latter ones give better performance, only worse)
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Rust
Assuming that replacement with
X
is per character.yay, a oneliner
With nightly feature
When you're a caveman and reimplement
all
:When you're a caveman and reimplement
.zip()
(none of the latter ones give better performance, only worse)