RosettaCodeData/Task/Binary-strings/Rust/binary-strings.rust

73 lines
2.8 KiB
Plaintext

use std::str;
fn main() {
//Create new string
let str = String::from("Hello world!");
println!("{}", str);
assert!(str == "Hello world!", "Incorrect string text");
//Create and assign value to string
let mut assigned_str = String::new();
assert!(assigned_str == "", "Incorrect string creation");
assigned_str.push_str("Text has been assigned!");
println!("{}", assigned_str);
assert!(assigned_str == "Text has been assigned!","Incorrect string text");
//String comparison, compared lexicographically byte-wise
//same as the asserts above
if str == "Hello world!" && assigned_str == "Text has been assigned!" {
println!("Strings are equal");
}
//Cloning -> str can still be used after cloning
let clone_str = str.clone();
println!("String is:{} and Clone string is: {}", str, clone_str);
assert!(clone_str == str, "Incorrect string creation");
//Copying, str won't be usable anymore, accessing it will cause compiler failure
let copy_str = str;
println!("String copied now: {}", copy_str);
//Check if string is empty
let empty_str = String::new();
assert!(empty_str.is_empty(), "Error, string should be empty");
//Append byte, Rust strings are a stream of UTF-8 bytes
let byte_vec = vec![65]; //contains A
let byte_str = str::from_utf8(&byte_vec).unwrap();
assert!(byte_str == "A", "Incorrect byte append");
//Substrings can be accessed through slices
let test_str = "Blah String";
let mut sub_str = &test_str[0..11];
assert!(sub_str == "Blah String", "Error in slicing");
sub_str = &test_str[1..5];
assert!(sub_str == "lah ", "Error in slicing");
sub_str = &test_str[3..];
assert!(sub_str == "h String", "Error in slicing");
sub_str = &test_str[..2];
assert!(sub_str == "Bl", "Error in slicing");
//String replace, note string is immutable
let org_str = "Hello";
assert!( org_str.replace("l", "a") == "Heaao", "Error in replacement");
assert!( org_str.replace("ll", "r") == "Hero", "Error in replacement");
//Joining strings requires a string and an &str or a two string one of which needs an & for coercion
let str1 = "Hi";
let str2 = " There";
let fin_str = str1.to_string() + str2;
assert!( fin_str == "Hi There", "Error in concatenation");
//Joining strings requires a string and an &str or two strings, one of which needs an & for coercion
let str1 = "Hi";
let str2 = " There";
let fin_str = str1.to_string() + str2;
assert!( fin_str == "Hi There", "Error in concatenation");
//Splits -- note Rust supports passing patterns to splits
let f_str = "Pooja and Sundar are up in Tumkur";
let split_str: Vec<&str> = f_str.split(' ').collect();
assert!( split_str == ["Pooja", "and", "Sundar", "are", "up", "in", "Tumkur"], "Error in string split");
}