Question
I am trying to compile Rust code that converts values from an integer to a string. I am following an older Rust for Rubyists tutorial that uses conversions such as "Fizz".to_str() and num.to_str() where num is an integer.
It seems that most or all uses of to_str() have been removed or deprecated. What is the current Rust way to convert an integer to a string?
These are the errors I get:
error: type `&'static str` does not implement any method in scope named `to_str`
error: type `int` does not implement any method in scope named `to_str`
Short Answer
By the end of this page, you will understand how Rust converts integers into strings using to_string() and format!, why older to_str() examples no longer compile, and how string conversion fits into everyday Rust programming.
Concept
In modern Rust, the usual way to convert an integer into a String is:
let num = 42;
let s = num.to_string();
The result is an owned String, which is Rust's growable string type.
Older Rust tutorials may show to_str(), but that API belonged to much earlier Rust versions and is no longer part of modern Rust. That is why current compilers report that the method does not exist.
Why this matters
Converting numbers to strings is common when you need to:
- print values
- build messages
- create filenames or IDs
- generate API responses
- combine numbers with text
Rust is strict about types. An integer like 42 and a string like "42" are different kinds of values. Rust requires you to convert explicitly when needed, which helps prevent bugs.
The modern tools
to_string()
Use this when you want a direct conversion to String.
= ;
= x.();
Mental Model
Think of Rust types as different container shapes.
- An integer is like a calculator result:
42 - A string is like a label printed on paper:
"42"
Even if they look similar to a human, Rust treats them as different things.
to_string() is like asking Rust to print the value onto a new piece of paper.
42becomes"42"truebecomes"true""Fizz"can be copied into an ownedString
So the rule of thumb is:
- if you need text you can own and store, use
to_string() - if you just want to display values inside a larger message,
format!is often more convenient
Syntax and Examples
Core syntax
let num = 123;
let s: String = num.to_string();
Example: integer to string
fn main() {
let num = 25;
let text = num.to_string();
println!("num = {}", num);
println!("text = {}", text);
}
What this does
numis an integernum.to_string()converts it into aStringtextnow contains"25"
Example: building a message
fn main() {
let = ;
= (, score);
(, message);
}
Step by Step Execution
Consider this example:
fn main() {
let num = 42;
let text = num.to_string();
println!("{}", text);
}
Step by step
1. let num = 42;
Rust creates an integer variable named num.
- value:
42 - type: inferred as an integer type such as
i32
2. let text = num.to_string();
Rust calls the to_string() method on num.
- it reads the integer value
42 - it creates a new owned string
- that string contains the characters
4and2
Now:
- is still an integer
Real World Use Cases
Logging and messages
let user_id = 101;
let log = format!("User {} signed in", user_id);
Applications often turn numbers into strings for logs and status messages.
Building file names
let report_id = 7;
let filename = format!("report-{}.txt", report_id);
This is useful for generated files, exports, and cache keys.
API responses
A server may convert numeric data into strings when building JSON manually or preparing display text.
let count = 3;
let message = format!("Found {} results", count);
CLI tools
Command-line programs often display numeric values as text.
let exit_code = 1;
(, exit_code);
Real Codebase Usage
In real Rust projects, developers usually use integer-to-string conversion in a few common patterns.
1. Formatting user-facing text
let retries = 3;
let msg = format!("Retry attempt {}", retries);
format! is often preferred when the number is only one part of a bigger message.
2. Returning a single consistent type
When if or match branches must all return the same type, to_string() is a simple fix.
let status = if is_admin {
"admin".to_string()
} else {
user_id.to_string()
};
3. Validation and error handling
Developers often convert numbers while constructing errors.
let age = 15;
let err = (, age);
Common Mistakes
1. Using old Rust methods like to_str()
Broken code:
let num = 5;
let s = num.to_str();
Why it fails:
to_str()is from old Rust examples- modern Rust uses
to_string()
Correct code:
let num = 5;
let s = num.to_string();
2. Confusing &str and String
Broken code:
let s: &str = 5.to_string();
Why it fails:
to_string()returns
Comparisons
Related ways to work with text in Rust
| Task | Best tool | Result | Example |
|---|---|---|---|
| Convert an integer to an owned string | to_string() | String | 42.to_string() |
| Build a larger string with values inside | format! | String | format!("ID: {}", 42) |
| Print a value | println! | output only | println!("{}", 42) |
| Use a string literal directly | string literal |
Cheat Sheet
Quick reference
Integer to string
let n = 42;
let s = n.to_string();
String literal to owned string
let s = "Fizz".to_string();
Build a string with values
let s = format!("Value: {}", 42);
Print without converting first
println!("{}", 42);
Rules to remember
to_str()is old Rust and should not be used in modern code.to_string()returns aString.- String literals like
"hello"are .
FAQ
Why does to_str() not work in modern Rust?
Older Rust versions had different APIs. In current Rust, to_str() is no longer the correct method for this task. Use to_string() instead.
How do I convert an integer to a string in Rust?
Use:
let s = 123.to_string();
Should I use to_string() or format!?
Use to_string() for simple direct conversion. Use format! when building a larger string with other text.
Is "hello" already a string in Rust?
Yes, but it is a &str, which is a string slice. If you need an owned String, use "hello".to_string().
Can println! print integers without converting them first?
Yes.
Mini Project
Description
Build a small Rust program that displays a label for a number. If the number is divisible by 3, show Fizz. Otherwise, convert the number into a string and display it. This mirrors a common beginner situation where different branches need to produce the same string type.
Goal
Create a program that returns a String from both branches of an if expression and prints the result.
Requirements
Requirement 1 Requirement 2 Requirement 3
Keep learning
Related questions
Accessing Cargo Package Metadata in Rust
Learn how to read Cargo package metadata like version, name, and authors in Rust using compile-time environment macros.
Associated Types vs Generic Type Parameters in Rust: When to Use Each
Learn when to use associated types vs generic parameters in Rust traits, with clear rules, examples, and practical API design advice.
Default Function Arguments in Rust: What to Use Instead
Learn how Rust handles default function arguments, why they are not supported, and practical patterns to achieve similar behavior.