Introduction to Rust string
The string is a data type in Rust that can be classified into two types, namely string literal written as &str, also known as string slice, and String object written as String, and the representation of &str is done by &[u8] that points to the UTP 8 sequence, and the data present in the string can be viewed using &str. The size of &str is fixed, meaning it cannot be resized. In contrast, the String object is encoded in UTF 8 sequence, and a heap memory allocation is done for the String object, and a String object’s size is changeable; that is, it keeps growing, and it is not a sequence that is terminated by a null.
The syntax to declare string literal or &str in Rust is as follows:
let variable_name:&str=”string_to_be_stored”;
where variable_name represents the name of the variable in which the string is stored,
string_to_be_stored is the string literal that is going to be stored inside the variable.
The syntax to declare String object or String in Rust is as follows:
String::new()
An empty string can be created using the above syntax
Or
String::from()
A string with a default value is created. The default value is passed as a parameter to the from the () method.
Working of String in Rust
- We make use of string literals or &str when the value of the string is known at compile time.
- A set of characters hardcoded into a variable is called a string literal.
- The module std::str consists of string literals.
- The string literals are static in nature by default; that is, they are valid as long as the program is running.
- The Standard library consists of the String object type.
- The standard library pub struct string defines the String object type.
- The size of the String object type is growable, and it can be resized.
- The String object type is mutable.
- The String object type is encoded in UTF 8 sequence.
- The string values at runtime can be represented using the String object.
- The heaps are used to store the String object.
Examples of Rust string
Here are the following examples mention below
Example #1
Rust program to demonstrate string literals in which two string literals are created, and values are stored inside the string literals and then displayed as the output on the screen:
fn main()
{
//defining a string literal to store the value of first string
let firststring:&str="Welcome to";
//defining a string literal to store the value of second string
let secondstring:&str = "EDUCBA";
//displaying the value of string literals as the output on the screen
println!("The string stored using string literal is : {} {}",firststring,secondstring);
}
The output of the above program is as shown in the snapshot below:
In the above program, we are creating a string literal called firststring to store a string value. Then we are creating another string literal called secondstring to store another string value.
Then we are displaying the two string values stored in the two string literals as the output on the screen.
Example #2
Rust program to demonstrate string object in which an empty string is created using new() method and another string object is created using from() method and the string to be stored in the second string object is passed as a parameter to the from() method and then the length of the two strings are displayed as the output on the screen by using len() method:
fn main()
{
//defining a string object to create an empty string
let firststring = String::new();
//defining a string object to store the value of a string
let secondstring = String::from("EDUCBA");
//displaying the length of the string objects in the two strings as the output on the screen
println!("The length of the string stored in the firststring is : {} ",firststring.len());
println!("The length of the string stored in the secondstring is : {} ",secondstring.len());
}
The output of the above program is as shown in the snapshot below:
In the above program, we are creating an empty string called the firststring using the new() method. Hence we are creating another string called secondstring using from the () method. The value of the string to be stored in the secondstring is passed as a parameter to the from() method. Then we are making use of the len() function to find the length of each string. The length of the firststring and the length of the secondstring is displayed as the output on the screen.
Example #3
Rust program to demonstrate string object in which two string objects are created using from() method and the strings to be stored in the two string objects are passed as a parameter to the from() method and then the length of the two strings are displayed as the output on the screen by using len() method:
fn main()
{
//defining a string object to store the value of a string
let firststring = String::from("Welcome to");
//defining a string object to store the value of another string
let secondstring = String::from("EDUCBA");
//displaying the length of the string objects in the two strings as the output on the screen
println!("The length of the string stored in the firststring is : {} ",firststring.len());
println!("The length of the string stored in the secondstring is : {} ",secondstring.len());
}
The output of the above program is as shown in the snapshot below:
In the above program, we are creating two string objects called firststring and secondstring using from the () method. The value of the strings to be stored in the firststring and secondstring is passed as a parameter to the from() method. Then we are making use of the len() function to find the length of each string. The length of the firststring and the length of the secondstring is displayed as the output on the screen.
Conclusion
In this article, we have learned the concept of String literal and String Object in Rust through definition, syntax, and working String literal and String Object of in Rust with corresponding programming examples and their outputs to demonstrate them.
Recommended Articles
This is a guide to Rust string. Here we discuss the concept of String literal and String Object in Rust through definition, syntax, and working String literal. You may also have a look at the following articles to learn more –
41 Online Courses | 13 Hands-on Projects | 322+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses