Rust-String Flashcards
Classifications of the String data type
String literal(&str)
String Object(String)
When is the String literal used?
String literals are used when the value of a string is know at compile time.
String literals are a set of characters, which are hardcoded into a variable.
String literals are also known as string slices
Give an example of a string literal declaration
let company:&str = “TutorialsPoint”;
let first_name:&str = “Prince”;
Describe String object
The String object type can be used to represent string values that are provided at runtime
Where is the string object allocated?
Heap
How do create an empty string object
String::new();
Give an example of a program using the string object
fn main(){
let empty_string = String::new();
println!(“length is {}”,empty_string.len());
let content_string = String::from(“TutorialsPoint”);
println!(“length is {}”,content_string.len());
}
Mention common methods of the string object
new()
to_string()
replace()
push()
push_str()
len()
trim()
split_whitespace()
split()
chars()
Describe the to_string()
Converts the given value to a String.
Describe replace()
Replaces all matches of a pattern with another string.
Describe push() and push_str()
push() - Appends the given char to the end of this String.
push_str() - Appends a given string slice onto the end of this String.
Describe split() and chars()
split() –Returns an iterator over substrings of this string slice, separated by characters matched by a pattern.
chars() – Returns an iterator over the chars of a string slice.
Give an example of the string object with using at least two of its method
fn main(){
let mut z = String::new();
z.push_str(“hello”);
println!(“{}”,z);
}
You can convert a string literal to a string object how
let name = “Prince”.to_string();