Strings and Numbers in Rust
by Hexagon, 3 min read rust guide
In this post, we'll explore text and numbers in Rust. Why are there two string types? Why can't you get the third letter of a string with text[2]? And what happens when an integer gets too big? Let's find out.
Two Kinds of Strings
Rust has two main string types:
Stringis an owned string. It lives on the heap, and can grow and change.&str(pronounced "string slice") is a borrowed view into some text. It can point into aString, or to text that's built into the program.
A simple rule of thumb: use &str for function parameters, and String when
you need to keep or build text.
Converting between them
Building Strings
Common String Methods
find returns Some(3) instead of a plain 3, because there might not be a
match. Where JavaScript's indexOf returns -1, Rust returns None. We'll
look at this properly in the article about enums.
Strings Are UTF-8
All strings in Rust are UTF-8. This has a consequence that surprises most newcomers: you can't index a string by position.
The reason is that one character can take more than one byte. "é" is two
bytes, and "🦀" is four. .len() gives you the number of bytes, not
characters. If Rust let you write word[0], it would either be slow, as it
would have to count from the start, or give you half a character.
Instead, you say what you want:
Slicing with &word[1..4] works on byte positions. If a position lands in the
middle of a character, the program panics, so be careful when slicing text that
isn't plain ASCII.
Numbers
Arithmetic works as you'd expect, with one difference from JavaScript: dividing two integers gives an integer.
No mixing of types
Rust never converts number types behind your back. You can't add an i32 to an
f64, or even an i32 to an i64, without converting one of them first:
as always succeeds, but it can lose information. 300 as u8 gives 44, and
3.9 as i32 gives 3. When a conversion might not fit, use try_from, which
tells you if it failed:
Math functions
In Rust, math functions are methods on the numbers themselves:
The _f64 and _i32 suffixes tell Rust which type a number literal is, so it
knows which methods are available.
Integer Overflow
What happens if a number gets too big for its type? In JavaScript, you lose precision silently. In Rust, it depends:
- In a debug build (
cargo run), the program panics, stopping with an error. - In a release build (
cargo run --release), the number wraps around, so255_u8 + 1becomes0.
As neither is usually what you want, Rust has methods that make the choice explicit:
Use checked_ when overflow means something went wrong, saturating_ when the
maximum is a sensible answer, and wrapping_ for things like hashes and
checksums, where wrapping is the point.
Floating Point
f64 is the same kind of number that JavaScript uses for everything, so it has
the same quirks:
For money, use integers of the smallest unit, like cents.
Parsing Numbers
To turn text into a number, use parse. You have to say which type you want,
and handle the case where the text isn't a number:
Result is how Rust handles anything that can fail. It's the topic of a whole
article later in the series.
Hexadecimal and Binary
That's a lot of ground. Take your time, experiment, and come back to this article when you run into strings that won't do what you want.