To convert an integer to string in Python, use the str() function. This function takes any data type and converts it into a string, including integers. Use the syntax print(str(INT)) to return the int as a str , or string.
JavaScript Number toString() The toString() returns a number as a string.
Convert int to string in C using sprintf() function The C library sprintf() function allows us to convert integers into a formatted string.
Use to_string()
(running example here):
let x: u32 = 10;
let s: String = x.to_string();
println!("{}", s);
You're right; to_str()
was renamed to to_string()
before Rust 1.0 was released for consistency because an allocated string is now called String
.
If you need to pass a string slice somewhere, you need to obtain a &str
reference from String
. This can be done using &
and a deref coercion:
let ss: &str = &s; // specifying type is necessary for deref coercion to fire
let ss = &s[..]; // alternatively, use slicing syntax
The tutorial you linked to seems to be obsolete. If you're interested in strings in Rust, you can look through the strings chapter of The Rust Programming Language.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With