Updated June 1, 2023
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 it is stored in heap memory with dynamic memory allocation. In contrast to a string literal, a String object in Rust has a dynamically changing size as it grows, and it is not terminated by a null character.
The syntax to declare string literal or &str in Rust is as follows:
let variable_name:&str="string_to_be_stored";
In the given program, the term “variable_name” represents the actual name of the variable that stores the string.
string_to_be_stored is the string literal that will be stored inside the variable.
The syntax to declare a String object or String in Rust is as follows:
String::new()
We can create an empty string using the above syntax.
Or
String::from()
We create a string with a default value by passing the default value as a parameter to the from() method.
Working of String in Rust
- We use string literals or &str when the value of the string is known at compile time.
- We call a set of characters hardcoded into a variable 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 can be resized.
- The String object type is mutable.
- The String object type in Rust encodes strings in UTF-8 sequence.
- To represent string values at runtime, we can use the String object.
- In Rust, the String object stores its data on the heap.
Examples of Rust string
Here are the following examples mentioned below
Example #1
Here is an example of a Rust program that demonstrates the use of string literals. It creates two string literals, stores values inside them, and then displays 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 given program, we use a string literal named “firststring” to store a specific string value. Then we create another string literal called secondstring to store another string value.
Then we display the two string values stored in the two string literals as the output on the screen.
Example #2
Here is an example of a Rust program that demonstrates the use of string objects. It creates an empty string using the new() method and another string object using the from() method. We pass the string that needs to be stored in the second string object as a parameter to the from() method. Finally, it displays the lengths of the two strings as the output on the screen using the 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 create an empty string called the firststring using the new() method. Hence we are creating another string called secondstring using the () method. To store the value of the string in the variable “secondstring,” we pass it as a parameter to the from() method. Then we use the len() function to find the length of each string. The program will output the lengths of the strings “firststring” and “secondstring” on the screen.
Example #3
Here is an example of a Rust program that demonstrates the use of String objects. The program creates two string objects by using the from() method and passing the strings as parameters to store them. Finally, it displays the length of the two strings using the len() method, and prints the output on the screen.
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 given program, we create two string objects, “firststring” and “secondstring,” using the from() method. To store the values of the strings in the variables “firststring” and “secondstring,” we pass them as parameters to the from() method. Then we use the len() function to find the length of each string. The program will display the length of the first string and the length of the second string 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 in Rust with corresponding programming examples and their outputs to demonstrate them.
Recommended Articles
We hope that this EDUCBA information on “Rust string” was beneficial to you. You can view EDUCBA’s recommended articles for more information.