Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Strings | Rust Fundamentals lesson | BraveProgrammer
  1. Courses
  2. /
  3. Rust Fundamentals

Lesson 12 of 50

·rust

Strings

13 minutes

Course progress24%

Strings

Rust has two primary string types, each serving different purposes.

String vs &str

String

  • Owned, growable, heap-allocated text
  • Mutable by default (if declared mut)
  • Created with String::from() or .to_string()

&str

  • Borrowed string slice
  • Fixed size, immutable
  • String literals are &str
 :  = ::(\
let
s1
String
String
from
"hello\"); // Owned let s2: &str = \"world\"; // Borrowed (literal) let s3: &str = &s1[..]; // Slice of String

Working with Strings

fn main() {
    let mut s = String::from(\"Hello\");
    
    // Append string
    s.push_str(\", \");
    
    // Push single character
    s.push('W');
    
    // Concatenation with +
    let s2 = s + \"orld\";  // \"Hello, World\"
    
    // Format macro
    let s3 = format!(\"{}, {}\", s2, \"Rust\");
}

String Operations

Length and Capacity

let s = String::from(\"hello\");
println!(\"Length: {}\", s.len());                // 5 bytes
println!(\"Capacity: {}\", s.capacity());          // Allocated space

Iteration

let s = \"hello\";

// Bytes
for b in s.as_bytes() {
    println!(\"Byte: {}\", b);
}

// Characters (handles Unicode)
for c in s.chars() {
    println!(\"Char: {}\", c);
}

Search and Replace

let s = \"hello world\";
println!(\"Contains 'world': {}\", s.contains(\"world\"));  // true
println!(\"Starts with 'hello': {}\", s.starts_with(\"hello\")); //true

let replaced = s.replace(\"world\", \"Rust\");
println!(\"Replaced: {}\", replaced);  // \"hello Rust\"

Important: String Indexing

You cannot index strings with integers directly:

let s = String::from(\"hello\");
// let c = s[0];  // ERROR!

Why? Strings are UTF-8 encoded, and characters can be multiple bytes. Use slicing or iteration instead:

let first_char = &s[0..1];  // Valid

Unicode Considerations

let s = \"hello\";           // Purely ASCII
println!(\"Len: {}\", s.len());  // 5

let emoji = \"🦀\";          // 4 bytes
println!(\"Len: {}\", emoji.len());  // 4 (bytes), not 1
println!(\"Chars: {}\", emoji.chars().count());  // 1 (character)

String Conversion

let num = 42;
let s = num.to_string();  // \"42\"

let s2 = String::from(\"123\");
let num2: i32 = s2.parse().unwrap();  // 123

Best Practices

  1. Use &str for function parameters when possible
  2. Use String when you need to own the data
  3. Remember strings are UTF-8
  4. Use slicing, not indexing
  5. Use .to_string() for easy conversions
Make it stickPractice what you've learned1 quiz · 1 practical test · Optional activities from Rust FundamentalsBrowse activities
Previous lessonSlicesContinue to lesson 13StructsBack to Rust Fundamentals curriculum