From 14a8fb3329dd7e6f962e50ff119f5c117d3a79c4 Mon Sep 17 00:00:00 2001 From: dqnid Date: Wed, 6 Nov 2024 23:01:45 +0100 Subject: [PATCH] feat(str_types) --- src/controlflow.rs | 1 - src/main.rs | 18 ++++++++++-------- src/str_types.rs | 29 +++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 9 deletions(-) create mode 100644 src/str_types.rs diff --git a/src/controlflow.rs b/src/controlflow.rs index 9e18fdd..c214261 100644 --- a/src/controlflow.rs +++ b/src/controlflow.rs @@ -8,7 +8,6 @@ pub fn control_flow_module() { println!("{count}"); count += 1; - if count >= BREAKPOINT { break count; } diff --git a/src/main.rs b/src/main.rs index 78be55e..f27c426 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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(); } diff --git a/src/str_types.rs b/src/str_types.rs new file mode 100644 index 0000000..c36cd34 --- /dev/null +++ b/src/str_types.rs @@ -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 = my_string.chars().collect(); + chars.sort(); + chars.dedup(); // remove duplicates + + // Initially empty strings are stored as a vector of bytes Vec 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); +}