Variables and Mutability in Rust

InstructorPascal Precht

Share this video with your friends

Send Tweet

In this lesson we'll learn how to initialise variables in Rust and how to make them mutable using the mut keyword so they can be changed throughout the program.

J. Matthew
~ 5 years ago

The immutable-by-default feature would make my buddy so happy...

println!("{}", name);

I'm baffled by the above placeholder syntax, though. It seems like an odd way to interpolate variables into strings. Compared with JavaScript's classic string concatenation "Hello, " + name + "!" or as an ES6 template literal Hello, ${name}!, this seems more roundabout and harder to read. Why does Rust do it this way?

J. Matthew
~ 5 years ago

Also, a quick transcript note: I noticed the last code section has let mutname = "Pascal"; rather than let mut name = "Pascal";.

Pascal Prechtinstructor
~ 5 years ago

Compared with JavaScript's classic string concatenation "Hello, " + name + "!" or as an ES6 template literal Hello, ${name}!, this seems more roundabout and harder to read. Why does Rust do it this way?

Hey Matthew,

that is a very interesting question. So first of all, I'm not a Rust compiler expert, so what I'm saying now might not be the actual reason, possibly only one of the reasons:

It turns out that println!() macro takes advantage of Rust's format!() macro: https://doc.rust-lang.org/std/macro.format.html

The format macro actually supports different types of formatters and placeholders. The lesson only covers the basic one ({}) but you might for example end up using the debug placeholder ({:?}) or even the pretty printed debug placeholder ({:#?}).

^ This alone already make "simple" string concatenation impossible. On top of that, it seems the compiler needs the first argument to be a string literal and anything that needs interpolation, as additional arguments so it can perform validity checks at compile time.

From the official docs (https://doc.rust-lang.org/std/fmt/):

From these, you can see that the first argument is a format string. It is required by the compiler for this to be a string literal; it cannot be a variable passed in (in order to perform validity checking). The compiler will then parse the format string and determine if the list of arguments provided is suitable to pass to this format string.

Notice that this is very similar to sprintf() fprintf() and printf(), functions in other languages.

Hope this makes a bit of sense.

J. Matthew
~ 5 years ago

^ This alone already make "simple" string concatenation impossible. On top of that, it seems the compiler needs the first argument to be a string literal and anything that needs interpolation, as additional arguments so it can perform validity checks at compile time.

Ah, that does make sense (the general concepts I get, at any rate). That behavior seems in line with Rust's focus on catching as many possible issues as possible at compile time. So in this case sacrificing some legibility and convenience for the sake of code integrity. I can get behind that. Thanks for the explanation!

Pascal Prechtinstructor
~ 5 years ago

Nice! Happy I could help :)