June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -1,72 +1,71 @@
use std::str;
fn main() {
//Create new string
let str = String::from("Hello world!");
println!("{}", str);
assert!(str == "Hello world!", "Incorrect string text");
// Create new string
let string = String::from("Hello world!");
println!("{}", string);
assert_eq!(string, "Hello world!", "Incorrect string text");
//Create and assign value to string
// 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!");
assert_eq!(assigned_str, "", "Incorrect string creation");
assigned_str += "Text has been assigned!";
println!("{}", assigned_str);
assert!(assigned_str == "Text has been assigned!","Incorrect string text");
assert_eq!(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!" {
// String comparison, compared lexicographically byte-wise same as the asserts above
if string == "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");
// 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");
//Copying, str won't be usable anymore, accessing it will cause compiler failure
let copy_str = str;
// Copying, string won't be usable anymore, accessing it will cause compiler failure
let copy_str = string;
println!("String copied now: {}", copy_str);
//Check if string is empty
// 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
// Append byte, Rust strings are a stream of UTF-8 bytes
let byte_vec = [65]; // contains A
let byte_str = str::from_utf8(&byte_vec).unwrap();
assert!(byte_str == "A", "Incorrect byte append");
assert_eq!(byte_str, "A", "Incorrect byte append");
//Substrings can be accessed through slices
// 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");
assert_eq!(sub_str, "Blah String", "Error in slicing");
sub_str = &test_str[1..5];
assert!(sub_str == "lah ", "Error in slicing");
assert_eq!(sub_str, "lah ", "Error in slicing");
sub_str = &test_str[3..];
assert!(sub_str == "h String", "Error in slicing");
assert_eq!(sub_str, "h String", "Error in slicing");
sub_str = &test_str[..2];
assert!(sub_str == "Bl", "Error in slicing");
assert_eq!(sub_str, "Bl", "Error in slicing");
//String replace, note string is immutable
// 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");
assert_eq!(org_str.replace("l", "a"), "Heaao", "Error in replacement");
assert_eq!(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
// Joining strings requires a `String` and an &str or a two `String`s 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");
assert_eq!(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
// Joining strings requires a `String` and an &str or two `Strings`s, 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");
assert_eq!(fin_str, "Hi There", "Error in concatenation");
//Splits -- note Rust supports passing patterns to splits
// 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");
let split_str: Vec<_> = f_str.split(' ').collect();
assert_eq!(split_str, ["Pooja", "and", "Sundar", "are", "up", "in", "Tumkur"], "Error in string split");
}