Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
29
Task/String-comparison/Rust/string-comparison.rust
Normal file
29
Task/String-comparison/Rust/string-comparison.rust
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
use std::ascii::AsciiExt; // for case insensitives only
|
||||
|
||||
fn main() {
|
||||
// only same types can be compared
|
||||
// String and String or &str and &str
|
||||
// exception: strict equality and inequality also work on &str and String
|
||||
let a: &str = "abc";
|
||||
let b: String = "Bac".to_owned();
|
||||
|
||||
// Strings are coerced to &str when borrowed and needed
|
||||
if a == b { println!("The strings are equal") }
|
||||
if a != b { println!("The strings are not equal") }
|
||||
if a > &b { println!("The first string is lexically after the second") }
|
||||
if a < &b { println!("The first string is lexically before the second") }
|
||||
if a >= &b { println!("The first string is not lexically before the second") }
|
||||
if a <= &b { println!("The first string is not lexically after the second") }
|
||||
|
||||
// case-insensitives:
|
||||
|
||||
// equality
|
||||
// this avoids new allocations
|
||||
if a.eq_ignore_ascii_case(&b) { println!("Both strings are equal when ignoring case") }
|
||||
|
||||
// everything else, create owned Strings, then compare as above
|
||||
let a2 = a.to_ascii_uppercase();
|
||||
let b2 = b.to_ascii_uppercase();
|
||||
|
||||
// repeat checks
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue