Transcript from the "Floats & Mutability" Lesson
>> 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: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.