Lädt...


🔧 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 Stringto &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 Stringwhen 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

🔧 Answer: How do I convert from a char array [char; N] to a string slice &str?


📈 32.14 Punkte
🔧 Programmierung

🔧 Python Bytes to String – How to Convert a Str to Bytes and Back Again


📈 31.43 Punkte
🔧 Programmierung

🔧 Python str() Function – How to Convert to a String


📈 30.12 Punkte
🔧 Programmierung

🕵️ Rust Programming Language Standard Library up to 127.0 str::repeat memory corruption


📈 28.92 Punkte
🕵️ Sicherheitslücken

🔧 Java String Management: String Pool vs String Heap Explained


📈 28.04 Punkte
🔧 Programmierung

🔧 CSS Text Handling: text-overflow, overflow-wrap, and More!


📈 25.61 Punkte
🔧 Programmierung

🔧 Why is it Discouraged to Accept `&String`, `&Vec`, or `&Box` as Function Arguments in Rust?


📈 23.54 Punkte
🔧 Programmierung

📰 Python OPP, and Why repr() and str() Matter


📈 23.4 Punkte
🔧 AI Nachrichten

🔧 How to Integrate OpenAI for Text Generation, Text-to-Speech, and Speech-to-Text in .NET


📈 22.37 Punkte
🔧 Programmierung

🔧 ### Introduction to Programming: Mastering File Handling and Exploring Error Handling


📈 21.84 Punkte
🔧 Programmierung

🔧 Day 3: File Handling and Error Handling


📈 21.84 Punkte
🔧 Programmierung

🍏 Plain Text Editor 1.2.1 - Simple distraction-free text editor without any rich text nonsense.


📈 21.06 Punkte
🍏 iOS / Mac OS

🔧 Facing an issue in froala text editor, style of the text is lost when the text is cut


📈 21.06 Punkte
🔧 Programmierung

📰 Offizieller "Video-Leak" zeigt Samsung Galaxy S7 im strömenden Regen


📈 20.77 Punkte
📰 IT Security Nachrichten

📰 Offizieller "Video-Leak" zeigt das Samsung Galaxy S7 im strömenden Regen


📈 20.77 Punkte
📰 IT Security Nachrichten

📰 Microsoft kauft 10 Millionen synthetische DNA-Stränge - zum Speichern von Daten


📈 20.77 Punkte
📰 IT Nachrichten

🕵️ Crestron DM-TXRX-100-STR bis 1.3039 objresp.authenabled Client schwache Authentisierung


📈 20.77 Punkte
🕵️ Sicherheitslücken

🕵️ Crestron DM-TXRX-100-STR bis 1.3039 JSON API Call Handler erweiterte Rechte


📈 20.77 Punkte
🕵️ Sicherheitslücken

🕵️ Crestron DM-TXRX-100-STR bis 1.3039 X.509 Certificate Test schwache Authentisierung


📈 20.77 Punkte
🕵️ Sicherheitslücken

🕵️ Crestron DM-TXRX-100-STR bis 1.3039.00040 Cross Site Request Forgery


📈 20.77 Punkte
🕵️ Sicherheitslücken

🕵️ Crestron DM-TXRX-100-STR bis 1.3039 index.html schwache Authentisierung


📈 20.77 Punkte
🕵️ Sicherheitslücken

📰 Offizieller "Video-Leak" zeigt Samsung Galaxy S7 im strömenden Regen


📈 20.77 Punkte
📰 IT Security Nachrichten

📰 Offizieller "Video-Leak" zeigt das Samsung Galaxy S7 im strömenden Regen


📈 20.77 Punkte
📰 IT Security Nachrichten

📰 Microsoft kauft 10 Millionen synthetische DNA-Stränge - zum Speichern von Daten


📈 20.77 Punkte
📰 IT Nachrichten

🕵️ Crestron DM-TXRX-100-STR bis 1.3039 objresp.authenabled Client schwache Authentisierung


📈 20.77 Punkte
🕵️ Sicherheitslücken

🕵️ Crestron DM-TXRX-100-STR bis 1.3039 JSON API Call Handler erweiterte Rechte


📈 20.77 Punkte
🕵️ Sicherheitslücken

🕵️ Crestron DM-TXRX-100-STR bis 1.3039 X.509 Certificate Test schwache Authentisierung


📈 20.77 Punkte
🕵️ Sicherheitslücken

matomo