Rust
December 2, 2026 6 min read

Strings and String Slices in Rust

HS
HEMANT SINGH
Technical Writer & Educator

Lesson 8: Strings and String Slices in Rust

1. What is a String?

A string is a sequence of characters used to store text.

Examples:

"Hello"
"Rust Programming"
"Welcome to Capscom Technology"

In Rust, the two commonly used string types are:

  1. String
  2. &str

Let's understand both.

2. String Slice (&str)

A string slice is a reference to string data.

Example:

fn main() {
let name: &str = "Hemant";

println!("Name: {}", name);
}

Output:

Name: Hemant

Here:

  1. name is a variable.
  2. &str is the string slice type.
  3. "Hemant" is the string value.

A string literal such as "Hemant" has the type &'static str. This means the text is available for the entire program.

Important Points

  1. &str is a borrowed reference to text.
  2. A string slice does not own the text.
  3. It is commonly used when a function only needs to read text.
  4. A string slice can refer to a part of a string.

3. String Type

String is an owned, growable string type.

Unlike a string literal, a String can be modified and expanded.

We can create a String using String::from() or .to_string().

Example 1: Using String::from()

fn main() {
let name = String::from("Hemant");

println!("Name: {}", name);
}

Output:

Name: Hemant

Example 2: Using to_string()

fn main() {
let language = "Rust".to_string();

println!("Language: {}", language);
}

Output:

Language: Rust

Both examples create an owned String.

4. Difference Between String and &str


Feature

String

&str

Ownership

Owns its text

Borrows text

Size

Growable

Fixed view of text

Modification

Can be modified

Cannot modify through the slice

Memory

Owns its string data

Refers to existing string data

Example

String::from("Rust")

"Rust"

Example:

fn main() {
let name1: &str = "Hemant";

let name2: String = String::from("Hemant");

println!("{}", name1);
println!("{}", name2);
}

Both print the same text, but they have different ownership characteristics.

5. Modifying a String

We can modify an owned String if it is declared as mutable.

Example:

fn main() {
let mut name = String::from("Hemant");

name.push('!');
println!("{}", name);
}

Output:

Hemant!

The push() method adds one character to the end of a string.

Using push_str()

The push_str() method adds a string slice to the end of a String.

Example:

fn main() {
let mut language = String::from("Rust");

language.push_str(" Programming");

println!("{}", language);
}

Output:

Rust Programming

Difference:


Method

Purpose

push()

Adds one character

push_str()

Adds a string slice

6. Concatenating Strings

Concatenation means joining two or more strings.

Rust provides the + operator and the format! macro.

Using the + Operator

fn main() {
let first = String::from("Hello");
let second = String::from(" Rust");

let result = first + &second;

println!("{}", result);
}

Output:

Hello Rust

Important:

  1. The left operand of + is a String.
  2. The right operand is a reference to a string slice, &str.
  3. The first String is moved into the concatenation operation and is no longer available afterward.

Using format! Macro

The format! macro is useful when joining multiple strings.

fn main() {
let first = String::from("Hello");
let second = String::from("Rust");
let third = String::from("Programming");

let result = format!("{} {} {}", first, second, third);

println!("{}", result);

println!("{}", first);
}

Output:

Hello Rust Programming
Hello

Unlike the + example, format! does not move the original strings.

7. Finding String Length

Use the .len() method to find the length of a string in bytes.

Example:

fn main() {
let name = String::from("Rust");

println!("Length: {}", name.len());
}

Output:

Length: 4

Important: Bytes vs Characters

Rust's .len() method returns the number of bytes, not necessarily the number of characters.

Example:

fn main() {
let name = String::from("Hello");

println!("{}", name.len());
}

Output:

5

For many English letters, one character occupies one byte in UTF-8.

However, some Unicode characters use multiple bytes.

Example:

fn main() {
let text = String::from("नमस्ते");

println!("Bytes: {}", text.len());
println!("Characters: {}", text.chars().count());
}

The byte count and character count can be different.

8. Accessing String Characters

Unlike arrays, Rust does not allow direct indexing into a String.

This is not allowed:

fn main() {
let name = String::from("Rust");

// This will cause a compilation error.
println!("{}", name[0]);
}

Why?

Rust strings use UTF-8 encoding. A character can occupy more than one byte, so a numeric index may not represent a complete character.

Using chars()

We can use .chars() to access characters.

fn main() {
let name = String::from("Rust");

for character in name.chars() {
println!("{}", character);
}
}

Output:

R
u
s
t

Using bytes()

We can use .bytes() to access the individual UTF-8 bytes.

fn main() {
let name = String::from("Rust");

for byte in name.bytes() {
println!("{}", byte);
}
}

Output:

82
117
115
116

Remember:

  1. chars() iterates over Unicode scalar values.
  2. bytes() iterates over UTF-8 bytes.

9. String Slicing

A string slice allows us to access a portion of a string.

Syntax:

&string[start..end]

The starting index is included, but the ending index is excluded.

Example:

fn main() {
let name = String::from("RustProgramming");

let part = &name[0..4];

println!("{}", part);
}

Output:

Rust

Here:

  1. 0 is the starting byte index.
  2. 4 is the ending byte index.
  3. The result contains the first four bytes, which form "Rust".

Important: String slice indexes must fall on valid UTF-8 character boundaries. Otherwise, the program can panic at runtime.

10. Common String Methods

Rust provides many useful string methods.


Method

Purpose

.len()

Returns the number of bytes

.is_empty()

Checks whether the string is empty

.contains()

Checks whether text exists

.to_uppercase()

Converts text to uppercase

.to_lowercase()

Converts text to lowercase

.trim()

Removes whitespace from both ends

.replace()

Replaces matching text

.push()

Adds one character

.push_str()

Adds a string slice

.chars()

Iterates over characters

.bytes()

Iterates over UTF-8 bytes

Example

fn main() {
let mut text = String::from(" Hello Rust ");

println!("Length: {}", text.len());
println!("Empty: {}", text.is_empty());
println!("Contains Rust: {}", text.contains("Rust"));
println!("Uppercase: {}", text.to_uppercase());
println!("Lowercase: {}", text.to_lowercase());
println!("Trimmed: {}", text.trim());

text = text.replace("Rust", "Programming");

println!("Replaced: {}", text);
}

11. Passing Strings to Functions

A function can accept a string slice using &str.

This is useful because it allows the function to read text without taking ownership of it.

Example:

fn greet(name: &str) {
println!("Hello, {}!", name);
}

fn main() {
let name = String::from("Hemant");

greet(&name);

println!("Name: {}", name);
}

Output:

Hello, Hemant!
Name: Hemant

Here, &name borrows the String as a string slice.

The original variable remains usable after the function call.

12. Returning a String from a Function

A function can return an owned String.

Example:

fn create_message() -> String {
String::from("Welcome to Rust")
}

fn main() {
let message = create_message();

println!("{}", message);
}

Output:

Welcome to Rust

Returning an owned String is a common way to create and send text from a function.

13. Important Notes

  1. String is an owned and growable string.
  2. &str is a borrowed string slice.
  3. String literals have the type &'static str.
  4. Use String::from() or .to_string() to create an owned string.
  5. Use push() to add a character.
  6. Use push_str() to add text.
  7. Use format! to combine strings without moving the original values.
  8. Rust strings use UTF-8 encoding.
  9. .len() returns bytes, not necessarily characters.
  10. Use .chars() to iterate over Unicode scalar values.
  11. Direct indexing into a String is not allowed.