The Rust Programming Language

Floats & Mutability

Richard Feldman

Richard Feldman

Vendr, Inc.
The Rust Programming Language

Check out a free preview of the full The Rust Programming Language course

The "Floats & Mutability" Lesson is part of the full, The Rust Programming Language course featured in this preview video. Here's what you'd learn in this lesson:

Richard briefly discusses that floats in Rust are binary and setting a variable using let means both const and immutable. The keyword mut can be added to make a variable mutable.

Preview
Close

Transcript from the "Floats & Mutability" Lesson

[00:00:00]
>> Let's talk about floats briefly that was a little bit about strings. You can see that string interpolation works with not only strings, but also with things like floats. So here we have x is 1.1, that's a float or a floating point number. We have y which is 2.2.

[00:00:15]
Then I can say x times y is and then multiply x by y and it'll just insert the answer right there. This will print out perhaps surprisingly, x times y is 2.42. And then a bunch of zeros, not a four, but it may or may not be surprising to you depending on if you've encountered the traditional IEEE binary floating point operations.

[00:00:34]
So, basically this is done because binary floats are very fast. So, if you wanted to have this to give you the expected answer that we would expect from normal math, that would be a significant performance cost because modern hardware can do this in like one instruction. Whereas if you want to do decimal math, it needs a lot more overhead than binary math does like this when it comes to floating point numbers.

[00:01:00]
So unfortunately, this is kinda what we're stuck with weaving like the programming community, at least when we want really high performance stuff. By the way, another thing to note is that a difference between println and main, so main is a function, println is a macro. So that's sorta what this exclamation point means.

[00:01:20]
We're not gonna really cover macros in this course, but the main thing to know about them is that macros are a way to do something that works a bit like a function call. But rather than calling the function, it's sorta doing some code transformation instead. So what println is actually doing is it's taking these arguments we're giving it the println macro.

[00:01:40]
And it's going to translate them at compile time into some number of other function calls, like, for example, stapling together these strings taking the result of this multiplication and then turning that into a string and then concatenating it with a string and so forth. We're not gonna define our own macros in this course.

[00:02:15]
Okay, let's talk about mutability briefly. So here I've said let x = 1.1, x = 2.2. Now this is a pretty normal looking piece of code in a language like JavaScript, in fact, this could be JavaScript code like valid JavaScript code, and it would work just fine. An x would start off being 1.1 and then it would get assigned to 2.2.

[00:02:37]
But in rust, that would not work. In fact, you would get this compile error cannot assign twice to immutable variable x. So in rust let works somewhat similarly to const in JavaScript, which is to say that once you declare this value, you're not allowed to reassign to it when you've used let.

[00:02:56]
We'll see in a second a way that you can make it so that it's allowed to be reassigned to you. But by default, you can't do that. Now worth noting that in this error message rust refers to this as an immutable variable. And they mean that they don't just mean that x is const, which in JavaScript means that you can't assign to it.

[00:03:13]
But it's also that x is immutable, which means that if x were a more complicated type, like here, we just have a floating point number but if we had some sorta structure that had multiple values inside it, you would not be able to mutate that structure like we would in JavaScript.

[00:03:25]
So in a sense, if this were JavaScript, it would be not only const x but also it would be assigned to something like object.freeze where basically it's saying yeah, this value right here, not only can we not really sign up, we also can't mutate it. So that's what let does in rust, it does both of those things.

[00:03:41]
Now, if you read the entire error message you might have seen the clue to how you can make this thing mutable. If you want, you can say let mut being short for mutable x. And then now not only can you reassign x but also you can mutate its contents if it had a more complicated type.

[00:03:58]
Then you can see there's a little help right here, make this binding mutable, make it be mut x and then it'll allow you to do that. So, to sum up, let and rust means both const and immutable and let mut means neither const nor immutable anymore. So rust likes to have things be immutable by default, which is why you need to add this extra keyword if you want to occasionally opt into mutation.

[00:04:20]
So in practice in rust programs, it's way more common to use let than to use let mut but from time to time, I do find myself using let mut if there's something that I want to make mutable.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now