feat(str_types)
This commit is contained in:
@@ -8,7 +8,6 @@ pub fn control_flow_module() {
|
||||
println!("{count}");
|
||||
|
||||
count += 1;
|
||||
|
||||
if count >= BREAKPOINT {
|
||||
break count;
|
||||
}
|
||||
|
||||
@@ -11,8 +11,9 @@
|
||||
//mod variablebindings;
|
||||
// mod types;
|
||||
//mod conversion;
|
||||
mod controlflow;
|
||||
// mod controlflow;
|
||||
// mod traits;
|
||||
mod str_types;
|
||||
|
||||
fn main() {
|
||||
// helloworld::hello_world_module();
|
||||
@@ -21,6 +22,7 @@ fn main() {
|
||||
//variablebindings::variable_bindings_module();
|
||||
//types::types_module();
|
||||
//conversion::conversion_module();
|
||||
controlflow::control_flow_module();
|
||||
// controlflow::control_flow_module();
|
||||
//traits::traits_exercise();
|
||||
str_types::str_types_module();
|
||||
}
|
||||
|
||||
29
src/str_types.rs
Normal file
29
src/str_types.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
pub fn str_types_module() {
|
||||
// Strings
|
||||
// &str is a slice &[u8]
|
||||
let my_string: &'static str = "A static string";
|
||||
|
||||
println!("Reversed: ");
|
||||
for word in my_string.split_whitespace().rev() {
|
||||
println!("> {}", word)
|
||||
}
|
||||
|
||||
// Can copy into a vector
|
||||
let mut chars: Vec<char> = my_string.chars().collect();
|
||||
chars.sort();
|
||||
chars.dedup(); // remove duplicates
|
||||
|
||||
// Initially empty strings are stored as a vector of bytes Vec<u8> in heap, is growable and not null terminated.
|
||||
let mut real_string = String::new();
|
||||
for c in chars {
|
||||
real_string.push(c);
|
||||
real_string.push_str(", ");
|
||||
}
|
||||
|
||||
println!("Real string: {}", real_string);
|
||||
|
||||
let chars_to_trim: &[char] = &[' ', ','];
|
||||
let real_string_trimmed: &str = real_string.trim_matches(chars_to_trim);
|
||||
|
||||
println!("Real string trimmed: {}", real_string_trimmed);
|
||||
}
|
||||
Reference in New Issue
Block a user