A Rustlings Introduction - intro1.rs and intro2.rs

We're going to presume that you're in your rustlings directory and you've typed in rustlings watch to get thing started.

When we are using rustlings watch we have started a program that will continually look at the directory in order and tell you what to do and what's going on. The output in your terminal changes based on what you are doing in the exercise files.

Right now, your terminal should look something like this. Rustlings is looking at your intro1.rs file and everything is working. In general, each file will have a commented line:

// I AM NOT DONE

If everything in the rust file in question is running and nothing is wrong, the presence of this line will stop Rustlings from moving onto the next file. This allows us to play with the contents in a file and allow Rustlings to tell us if we've broken anything.

Since this is the very first file, I'd like to call out some things in here of particular note. We are in the intro directory and in this directory is a README.

# Intro

Rust uses the `print!` and `println!` macros to print text to the console.

## Further information

- [Hello World](https://doc.rust-lang.org/rust-by-example/hello.html)
- [Formatted print](https://doc.rust-lang.org/rust-by-example/hello/print.html)

Each section of Rustlings will have a README with additional information that is pertinent to the section. For example we see here that we are dealing mainly with the print! and the println! macros and we have links to some additional reading on these topics.

Some takeaways from intro1.rs

  • print! prints stuff to the screen
  • println! prints stuff to the screen
  • It looks like we have to wrap all of our code in a fn main() {} like some other programming languages.

Let's go ahead and delete the // I AM NOT DONE and see what happens. Don't forget to save.

Rustlings has noticed that we've updated and because everything in intro1.rs is good to go, it will move onto the next file in sequence. And we have an error. This is the entire point of Rustlings. It will give you a thing to do and you have to fix the errors or make things happen in conjunction with the errors, the links in the README, and the hint function.

If you don't know what to do, you can always type hint into Rustlings and it will give you a hint on what to do.

The hint it gives me here is that I need to add an argument after the format string. I kind of gathered that I needed to from the error message:

error: 1 positional argument in format string, but no arguments were given
 --> exercises/intro/intro2.rs:8:21
  |
8 |     println!("Hello {}!");
  |                     ^^

That's not quite the most helpful thing, so we can check the formatted print documentation that they gave us in the README, and we have this helpful section.

// In general, the `{}` will be automatically replaced with any
// arguments. These will be stringified.
println!("{} days", 31);

Looking at that code in the documentation, I am going to draw the conclusion that what gets printed out to the screen when I run that bit of code is 31 days.

So let's add something to the code.

And we see that the error has gone away.

We can continue to play with this and experiment.

Looks like we can add additional things to format in here.

When you are ready, go ahead and delete that // I AM NOT DONE.

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