String Literal | ||||||||||||||||||||||||||||||||||||
two types of String data type
string literals are a set of characters, which are hardcoded into a variable. For string literals are found in module std::str string literals are also called as string slices example declares two string literals fn main() { let company:&str="TutorialsPoint"; let location:&str = "Hyderabad"; println!("company is : {} location :{}",company,location); }string literals are static by default string literals are guaranteed to be valid for the duration of the entire program. can also explicitly specify the variable as static fn main() { let company:&'static str = "TutorialsPoint"; let location:&'static str = "Hyderabad"; println!("company is : {} location :{}",company,location); } |
||||||||||||||||||||||||||||||||||||
String Object | ||||||||||||||||||||||||||||||||||||
the string object type is not a part of the core language defined as public structure in standard library pub struct String String is a growable collection is mutable and UTF-8 encoded type String object type can be used to represent string values that are provided at runtime String object is allocated in the heap syntax to create an empty String object String::new()creates a string with some value passed as parameter to the from() method String::from()common methods of String objects
new()
fn main(){ let mut z = String::new(); z.push_str("hello"); println!("{}",z); } to_string()
to access all methods of String object, convert a string literal to object type using the to_string() function
fn main(){ let name1 = "Hello TutorialsPoint, Hello!".to_string(); println!("{}",name1); } replace()
the replace() function takes two parameters
fn main(){ let name1 = "Hello TutorialsPoint, Hello!".to_string(); //String object let name2 = name1.replace("Hello","Howdy"); //find and replace println!("{}",name2); } as_str()
extracts a string slice containing the entire string
fn main() { let example_string = String::from("example_string"); print_literal(example_string.as_str()); } fn print_literal(data:&str ){ println!("displaying string literal {}",data); } push()
appends the given char to the end of this String
fn main(){ let mut company = "Tutorial".to_string(); company.push('s'); println!("{}",company); } push_str()
appends a given string slice onto the end of a String
fn main(){ let mut company = "Tutorials".to_string(); company.push_str(" Point"); println!("{}",company); } len()
returns the total number of characters in a string (including spaces)
fn main() { let fullname = " Tutorials Point"; println!("length is {}",fullname.len()); } trim()
removes leading and trailing spaces in a string
fn main() { let fullname = " Tutorials Point \r\n"; println!("Before trim "); println!("length is {}",fullname.len()); println!(); println!("After trim "); println!("length is {}",fullname.trim().len()); } split_whitespace()
splits the input string into different stringsreturns an iterator to iterate through the tokens fn main(){ let msg = "Tutorials Point has good tutorials".to_string(); let mut i = 1; for token in msg.split_whitespace(){ println!("token {} {}",i,token); i+=1; } } split()
returns an iterator over substrings of a string slice, separated by characters matched by a patternlimitation of the split() method is that the result cannot be stored for later use the collect method can be used to store the result returned by split() as a vector. fn main() { let fullname = "Kannan,Sudhakaran,Tutorialspoint"; for token in fullname.split(","){ println!("token is {}",token); } //store in a Vector println!("\n"); let tokens:Vec<&str>= fullname.split(",").collect(); println!("firstName is {}",tokens[0]); println!("lastname is {}",tokens[1]); println!("company is {}",tokens[2]); } chars()
individual characters in a string can be accessed using the chars method
fn main(){ let n1 = "Tutorials".to_string(); for n in n1.chars(){ println!("{}",n); } } |
||||||||||||||||||||||||||||||||||||
Concatenation of Strings with + operator | ||||||||||||||||||||||||||||||||||||
a string value can be appended to another string is called concatenation or interpolation the result of string concatenation is a new string object the + operator internally uses an add method the syntax of the add function takes two parameters the first parameter is self – the string object itself and the second parameter is a reference of the second string object //add function add(self, &str) -> String { // returns a String object }NOTE: this is the first example of a function signature example of concatenation fn main(){ let n1 = "Tutorials".to_string(); let n2 = "Point".to_string(); let n3 = n1 + &n2; // n2 reference is passed println!("{}",n3); }NOTE: to this point there has been no explanation of passing by value versus passing by reference converting a number to a string object fn main(){ let number = 2020; let number_as_string = number.to_string(); // convert number to string println!("{}",number_as_string); println!("{}",number_as_string=="2020"); }can also use a macro function named format fn main(){ let n1 = "Tutorials".to_string(); let n2 = "Point".to_string(); let n3 = format!("{} {}",n1,n2); println!("{}",n3); } |