Variables and Variable Assignment - variables1.rs

Let's start on variables1.rs.

When we look at the errors it's not quite intuitive, coming from Ruby. My initial reaction is that when we look at line 8, what do you mean not found in this scope? We're just making a new variable x, and setting five into it.

This is first big difference we will encounter in Rust as opposed to Ruby. We cannot just spin up a variable at will and store values in them. We have to say, hey, we are going to need a variable. That syntax will look like this:

let x = 5;

Yes, in Rust we have to end lines with a semicolon. Gross.  🤮

In Rust, we have to declare our variables before use, and let is the keyword with which we do so.

Save your file and now the error on line 9 is also gone because x now has a value, and the println! macro can interpolate the value of x properly.

Note: Right now you can think of the Rust not found in this scope to be somewhat similar to Ruby's NameError: undefined local variable error. They're essentially saying hey, I don't know of anything called that in this scope.

Let's delete that // I AM NOT DONE and get out of here.

Subscribe to Rust for Rubyists

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe