Good morning everyone, there is a new Rust lang series episode for today. We will talk little bit more about strings in Rust. So far we mostly worked with &str as string representation. Today we will mainly focus on String struct (std::string::String) from standard library and how it operates with &str. Usage simple as you will see soon.
string as &str
First create string as &str
let string_as_str_ref : &str = "string as str ref";
String is NOT &str
This we cannot do
let string_as_string : String;
string_as_string = string_as_str_ref;
# output
error: mismatched types
Create String from &str with to_string() method
But this we can do
let string_as_string: String = string_as_str_ref.to_string();
More with String
Now, let's go through basic String API for various operatiosn
Create empty String
let empty_string = String::new();
String from string
let empty_string = "".to_string();
Create String from string by using from method
let from_string = String::from("Rust Steemit series");
Append char to String
let mut str = String::new();
str.push('c');
Append string to String
let mut str = String::new();
str.push_str("Rust");
String from UTF-8 bytes (vec<u8>
)
When we deal with data we can have UTF-8 bytes which we want to transfer to String.
let four_stars_vec = vec![42,42,42,42];
let str = String::from_utf8(four_stars_vec).unwrap();
println!("{}",str);
# output
****
Get chars from string
let str = String::from("1234");
let chars = str.chars();
for ch in chars {
print!("{},", ch);
}
# output
1,2,3,4,
Truncate string at index (index char included)
let mut str = String::from("1234567890");
str.truncate(5);
println!("{}", str);
# output
12345
Remove char at index
let mut str = String::from("1234567890");
str.remove(5);
println!("{}", str);
# output
123457890
Insert char at index
let mut str = String::from("1234567890");
str.insert(5,'-');
println!("{}", str);
# output
12345-67890
String length
let len = str.len();
Empty string content
str.clear();
Check if String is empty
let is_empty = str.is_empty();
Working with multiple lines
Rust has some quite handy String API to work with String lines. Check this example
let multi_line_str = "Hi\n\nRust supports multi-line\n\nBye!";
for l in multi_line_text.lines() {
println!("{}",l);
}
#output
Hi
Rust supports multi-line
Bye!
String and &str
As String implements Deref<Target=str>
(we will talk in the future about it), we can pass string reference whereever reference to str is required. Check this piece of code below
fn print_str_ref(str_ref: &str) {
println!("{}", str_ref);
}
fn main() {
let string = String::from("That's last example for today!");
print_str_ref(&string);
}
## output
That's last example for today!
https://doc.rust-lang.org/std/string/struct.String.html#method.chars
That's all for today, thank you for your appreciations, feel free to comment and point out possible mistakes (first 24 hours works the best but any time is fine). Jesus bless your programming skills, use them wisely and see you next time.
Meanwhile you can also check official documentation to find more about discussed topics: