Rust Strings

Rust Tutorial

Rust Strings – Complete Beginner Guide

Strings in Rust are powerful but slightly different from other languages.

Rust provides two main string types:

  1. String (heap-allocated, growable)
  2. &str (string slice, borrowed reference)

Understanding the difference is essential for mastering Rust.

In this guide, you’ll learn:

  • What String and &str are

  • Stack vs heap memory

  • Creating and modifying strings

  • String slices

  • Common string methods

  • UTF-8 handling

  • Best practices

Let’s dive in


Two Types of Strings in Rust

TypeDescriptionStored
StringGrowable, owned stringHeap
&strBorrowed string sliceStack reference

What Is &str?

&str is a string slice (reference to a string).

Example:


 

This is:

  • A string literal

  • Stored in binary

  • Immutable

  • Type: &'static str


What Is String?

String is:

  • Owned

  • Heap allocated

  • Growable

  • Mutable (if declared mut)

Example:


 


Visual: Stack vs Heap

https://federicoterzi.com/posts/string1.JPEG

String contains:

  • Pointer

  • Length

  • Capacity

Data stored in heap.


Creating Strings

From literal


 

Using to_string()


 


String vs &str Conversion

String → &str


 

&str → String


 


Modifying Strings

Only String can be modified.


 


String Concatenation

Using +


 

  • s1 is moved here.

Using format! (Recommended)


 

  •  Does not move values
  •  Cleaner syntax

String Slicing


 

But careful:

Rust strings are UTF-8 encoded.

Indexing is byte-based, not character-based.


UTF-8 in Rust Strings

Rust strings store UTF-8 data.

Example:


 

Each character may use multiple bytes.

You cannot index directly:


 

Instead use:


 


Iterating Over Strings

Characters


 

Bytes


 


Common String Methods

MethodDescription
len()Length in bytes
push()Add character
push_str()Add string
replace()Replace text
trim()Remove whitespace
contains()Check substring
split()Split string

Example:


 


Ownership with Strings

Passing String to function moves ownership:


 

Better approach:


 

Use &str when possible.


Common Beginner Mistakes

  •  Confusing String and &str
  •  Trying to index string directly
  •  Forgetting ownership moves
  •  Using + without understanding move
  •  Ignoring UTF-8

Best Practices

  •  Use &str for function parameters
  •  Use format! for concatenation
  •  Prefer borrowing over cloning
  •  Understand UTF-8 indexing
  •  Keep string scopes small

Frequently Asked Questions (FAQs)

1. What is the difference between String and &str in Rust?

String is owned and heap-allocated.
&str is a borrowed string slice.


2. Why can’t I index a Rust string?

Because Rust strings are UTF-8 encoded, and characters may use multiple bytes.


3. How do I convert &str to String?

Use .to_string() or String::from().


4. How do I concatenate strings safely?

Use the format! macro.


5. Is String mutable by default?

No. You must declare it with mut.


Conclusion

Rust strings are powerful but require understanding:

  • Ownership

  • Borrowing

  • UTF-8 encoding

  • Heap vs stack

Once you master String and &str, Rust becomes much easier to work with.

You may also like...