Michael Krisher
4 min readJan 29, 2024

--

Rust Numbers in comparison to Ruby

In the last post, we discussed Strings. In this post, we are going to look at the differences between Ruby’ numbers and Rust’s various number types.

Numbers, either integers or floats, are pretty straightforward in Ruby. You simply declare the value and the interpreter will decide if it is an Integer or a Float. Example Ruby code below:

If you are familiar with Ruby, you may take for granted how easy it is to declare a numeric data type and not think about the differences between a whole integer and a float with a decimal. Not until you have to explicitly control precision maybe. Now let’s look at the Rust equivalent:

Woah! You may be asking what an i32 and f32 are. Rust is more explicit about numeric data declaration and it is wonderful. If you have written any heavy math in Ruby, you may have spent time figuring out where nulls were coming from, or why you were receiving Integers when you anticipated a Float. The Rust compiler will catch those differences and give clear and meaningful feedback. Let’s show an example:

--

--