feat(str_types)

This commit is contained in:
2024-11-06 23:01:45 +01:00
parent 7e0c21cc42
commit 14a8fb3329
3 changed files with 39 additions and 9 deletions

View File

@@ -8,7 +8,6 @@ pub fn control_flow_module() {
println!("{count}");
count += 1;
if count >= BREAKPOINT {
break count;
}

View File

@@ -5,22 +5,24 @@
// : /// Doc comment: generate library docs for the following item
// : //! Doc comment
//mod helloworld;
//mod primitives;
//mod customtypes;
// mod helloworld;
// mod primitives;
// mod customtypes;
//mod variablebindings;
// mod types;
//mod conversion;
mod controlflow;
// mod controlflow;
// mod traits;
mod str_types;
fn main() {
//helloworld::hello_world_module();
//primitives::primitives_module();
//customtypes::custom_types_module();
// helloworld::hello_world_module();
// primitives::primitives_module();
// customtypes::custom_types_module();
//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
View 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);
}