🔧 Handling Text in Rust: String and &Str
Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to
Unlike other programming languages, handling text in rust can be a bit complicated. This is due to some type-handling mechanism that rust forces us to adhere to in order to mitigate any problems that might result from the "text-structure and usage" at compile time. Rust uses two types to identify "text-based" characters. These are String and &Str. They are very similar in many ways but differ in terms of ownership, mutability and how they are represented in memory.
String Type
The String type
is a heap-allocated owned string. This means that its size is dynamic and can adjust to the needs at runtime.
Also, the String type is mutable which implies that you can either append characters or change characters in the String type
. This mutability is possible because the String type owns the memory that it uses.
for example:
let mut s= String::from("Hello");
s.push(", World"); //we modify the string s
println!("{}", s);
When we say the String type size is dynamic, we mean that it is allowed by rust to grow and shrink as needed as demonstrated in the simple example above.
&Str Type
The &Str type
on the other hand refers to a borrowed string slice. It is used to represent a view into a part of a string but it does not own the memory to which it point(because it is borrowed). Unlike the String type
, the &Str type
is an immutable reference usually alluded to a String type or a string literal. Therefore its data cannot be modified.
let s= "This is a rust example" //this is a &str
printlin!("{}", s)
let s= String::from("Rust is awesome"); //this is a String
let sliced_string: &str= &s[0..5]; //&str(string slice)
printlin!("{}", sliced_string)
String Literals
String literals
in rust are also &str types
. This means they are also stored the program's binary at compile time and are immutable.
for example,
let greeting= "Hello"; //this is a &str
Conversion between String
and &Str
You can convert a &str
to a String
using the String::from
function or the .to_string()
Method
let heading: &str="A letter to rust";
let heading_string:String= heading.to_string()
On the other hand conversion from String
to &str
is not as straight-forward as you will have to borrow the String content.
let heading= String::from("A letter to rust");
let slice_heading:&str= &heading; //borrow heading as a &str
Now that you know the difference between &str and String, when should you use them?
Use String
when you need:
- Ownership over the string
- To modify the string at some point
- To store a dynamic string i.e a string which you do not know the size at compile time
Use &Str
when you:
- Do not need to own the string
- Require a reference to the string without making a copy or clone
- Do not need to modify the string at any point
I hope this article has helped you understand more about &str
and String
and how they provide great flexibility when it comes to handling string data in different contexts.
🔧 Handling Text in Rust: String and &Str
📈 58.87 Punkte
🔧 Programmierung
🔧 Day 3: File Handling and Error Handling
📈 21.84 Punkte
🔧 Programmierung