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

72 lines
2.9 KiB
Text
Raw Permalink Normal View History

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