12. Сравнение строк

Питон:

def compare(a, b):
print("\n%r is of type %r and %r is of type %r"
% (a, type(a), b, type(b)))
if a < b: print('%r is strictly less than %r' % (a, b))
if a <= b: print('%r is less than or equal to %r' % (a, b))
if a > b: print('%r is strictly greater than %r' % (a, b))
if a >= b: print('%r is greater than or equal to %r' % (a, b))
if a == b: print('%r is equal to %r' % (a, b))
if a != b: print('%r is not equal to %r' % (a, b))
if a is b: print('%r has object identity with %r' % (a, b))
if a is not b: print('%r has negated object identity with %r' % (a, b))
compare('YUP', 'YUP')
compare('BALL', 'BELL')
compare('24', '123')

Раст:

// For case-insensitive compare only.
use std::ascii::AsciiExt;
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:
// everything else, create owned Strings, then compare as above
let a2 = a.to_ascii_uppercase();
let b2 = b.to_ascii_uppercase();
// equality
// this avoids new allocations
if a.eq_ignore_ascii_case(&b) {
println!("Both strings are equal when ignoring case")
}
if a2 == b2 {
println!("The strings are equal")
}
if a2 != b2 {
println!("The strings are not equal")
}
if a2 > b2 {
println!("The first string is lexically after the second")
}
if a2 < b2 {
println!("The first string is lexically before the second")
}
if a2 >= b2 {
println!("The first string is not lexically before the second")
}
if a2 <= b2 {
println!("The first string is not lexically after the second")
}
}

14. Форматирование строк

Питон:

str = 'Mary had a %s lamb.' % 'little'

Раст

fn main() {
println!("Mary had a {} lamb", "little");
// You can specify order
println!("{1} had a {0} lamb", "little", "Mary");
// Or named arguments if you prefer
println!("{name} had a {adj} lamb", adj="little", name="Mary");
}