You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
for i in0..100{println!("{}", i);}// Loop in reversefor i in(0..100).rev(){println!("{}", i);}
Match Statement
let country_code = 44;let country = match country_code {44 => "UK",46 => "Sweden",7 => "Russia",1..=1000 => "unknown",
_ => "invalid"};println!("The country with code {} is {}", country_code, country);
letmut my_vec:Vec<i32> = Vec::new();// Initialize vector of size n filled with zeroesletmut my_vec = vec![0;100];// Creates int vector for size 100 filled with zeroes
Add item in vector
letmut my_vec = Vec::new();
my_vec.push(1);// [1]
my_vec.push(2);// [1,2]// Insert at 0th position
my_vec.insert(0,3);// [3,1,2]// Insert at 1st position
my_vec.insert(1,4);// [3,4,1,2]
letmut count_vec:Vec<_> = map.iter().collect();// Sort the hashmap by key in ascending order
count_vec.sort_by(|a, b| a.0.cmp(b.0));// Sort the hashmap by value in ascending order
count_vec.sort_by(|a, b| a.1.cmp(b.1));// Sort the hashmap by value in descending order
count_vec.sort_by(|a, b| b.1.cmp(a.1));
Create & initialize string
let s = "Hello world";letmut str = String::new();// Uninitialized string
Get character at nth index in string
s.chars().nth(i).unwrap()
Update character at nth index in string
let s = "Hello world".to_string();let iter = s.chars();letmut new_s = String::new();for(i,mut c)in iter.enumerate(){if i == 1{ c = 'i';}
new_s.push(c);}println!("{}", new_s);
Substring
let s = "Hello world".to_string();println!("{}",&s[6..11]);// returns "world"