Skip to content

bits-of-rust/episode-004

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

episode-004

Where we bind a variable

Setup

We need to prepare

  • A project called variables
  • A println! call with an argument

Script

Variables are a way to reuse certain values in your program. We start from the a familiar println! expression.

println!("Hello, {}!", "world");

If we would also want to say Goodbye, world!, this could be achieved by adding the following line.

println!("Goodbye, {}!", "world");

But what if we would want to say hello and goodbye to the moon? We would have to change the argument to the two println!.

println!("Hello, {}!", "moon");
println!("Goodbye, {}!", "moon");

Which seem wasteful. Let's see how a variable can help in this situation.

With let we can introduces a variable subject with the value world, that is used as an argument in the println! call.

let subject = "world";

println!("Hello, {}!", subject);
println!("Goodbye, {}!", subject);

Saying goodbye to moon can be achieved by changing the subject to moon.

let subject = "moon";

And there you have it, we bound a variable.