Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Rust Strings


Strings

Strings are used to store text, surrounded by double quotes.

You have already learned that you can use the &str type to create a string:

Example

let greeting: &str = "Hello";
println!("{}", greeting);
Try it Yourself »

There are two main types of strings in Rust:

  • &str - is called "string slices", and is used for fixed text like "Hello"
  • String - used when you need a string that can change

In this chapter, you will mostly work with the String type because it is more flexible and can be changed over time.


Create a String

You can create a String from a string literal using the to_string() method or the String::from() function:

Example

let text1 = "Hello World".to_string();
Try it Yourself »

Example

let text2 = String::from("Hello World");
Try it Yourself »

It is up to you which one to choose - both to_string() and String::from() are very common in Rust.


Change a String

Strings are mutable, so you can change them if they are declared with mut.

Use push_str() to add text to a string:

Example

let mut greeting = String::from("Hello");
greeting.push_str(" World");
println!("{}", greeting); // Hello World
Try it Yourself »

Use push() to add one character:

Example

let mut word = String::from("Hi");
word.push('!');
println!("{}", word); // Hi!
Try it Yourself »

Concatenate Strings

You can combine strings using the format! macro:

Example

let s1 = String::from("Hello");
let s2 = String::from("World!");
let s3 = String::from("What a beautiful day!");
let result = format!("{} {} {}", s1, s2, s3);
println!("{}", result);
Try it Yourself »

You can also use the + operator to combine strings, but it can get messy with many values.

Example

let s1 = String::from("Hello");
let s2 = String::from("World!");
let s3 = String::from("What a beautiful day!");
let result = s1 + " " + &s2 + " " + &s3;
println!("{}", result);
Try it Yourself »

Note: You can only add a &str to a String with +. That is why &s2 and &s3 (a string slice) is used here.

Good to know: format! is often a better choice than using + for combining strings.


String Length

You can get the number of bytes in a string using the .len() method:

Example

let name = String::from("John");
println!("Length: {}", name.len()); // 4
Try it Yourself »


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.