Tooling
Learning objectives
- You know how to create and run a Rust application on your computer.
- You are familiar with the
cargo
command line tool. - You know how to use
cargo
to create a new project and add dependencies.
Running Rust on Your Computer
As we saw when completing the first exercise, we have an embedded code editor for testing out examples and writing and testing exercise solutions in the browser without the need to install Rust on your own computer. As such, it is possible to complete the course without installing Rust on your computer although we heavily suggest testing and running code on your own computer. The browser editor embedded into the course materials is a rather simple text editor and cannot provide code hints or debugging help that integrated development environments (IDEs) can. The browser editor is more suited for testing out examples or modifying small code snippets than for completing or debugging coding exercises.
The recommended way to install Rust on any system is using rustup. Rustup installs the Rust compiler (rustc
) and Cargo (cargo
), which is the Rust package manager and build tool. Follow the instructions on the rustup website to install Rust and Cargo on your computer.
The website for Rust documentation includes documentation for Cargo in the Cargo Book. The Cargo Book is a great resource for learning how to use Cargo and how to create and manage Rust projects, i.e. packages. Familiarize yourself with Cargo and how to create and run a Rust (binary) program from the command line using Cargo with the help of the First Steps with Cargo section of the Cargo Book.
Once you've read the first steps with Cargo, try to create a new Rust project using Cargo and run it from the command line.
Question not found or loading of the question is still in progress.
Setting up an IDE for Rust
VS Code is a free code editor with excellent support for Rust development through rust-analyzer. Many other editors and IDEs also have Rust support, and you are free to use them. But if you don't have a favorite IDE yet, we recommend starting with VS Code.
You can find installation instructions for VS Code from the webpage https://code.visualstudio.com/docs/setup/setup-overview and selecting from there your operating system for a platform-specific guide.
When in doubt, use an integrated development environment
There's no need to get stuck debugging course exercises in our very limited embedded code editor.
When using an integrated development environment (IDE), there is hardly any need to try and remember specific types or their methods as the editor can suggest them as long as you have at least a vague idea of what is available. With modern tools you don't have to remember so much of the details.
Installing the Rust extension
Once you have VS Code installed and opened, you can install the Rust extension by searching for it by the name rust-analyzer in the extensions tab. You can open the extensions tab by clicking on the extensions icon (the one with three blocks together and one separate block) in the left sidebar.
Instructions with images can be found in the VS Code documentation for Rust.
If you prefer another editor, you can find a list of editors with Rust support on Rust Tools site and in the rust-analyzer manual.
Our embedded editor can be a bit slow. A better option for testing out Rust code is the online Rust Playground that you can open through button in the embedded editor. An even better option is to use your computer as the playground, there you'll have the full support of your favorite IDE.
Luckily for those using VS Code, an extension named Rust Playground can be used to create a new playground on demand in the VS Code editor. The extension can be found by typing in its name in VS Code's extension search. The extension does not support rust-analyzer (yet), so there will be no code hints or other intellisense features, but it should still be faster than compiling code online.
Our embedded editor has the button to ease copy-pasting the example and exercise starter codes to your local editor.
REPL
A Read-Evaluate-Print-Loop (REPL) is a type of programming environment in which the user's interactive code input is executed and printed out. Testing out code in a REPL is a great way to learn a new language.
Rust does not have a built in REPL, but an excellent one called Evcxr Rust Repl has been made by the community (by Google to be precise, but it is open source).
We can use Cargo to easily install the REPL.
First however, we need to install a local copy of Rust's source code using rustup:
And then we can install the REPL:
And start the REPL:
Since evcxr might be a hard name to remember, you might be interested in adding an alias for the command.
In bash (default shell for most Linux distributions), this can be done for example by adding the line
to the file $HOME/.bashrc
which is executed every time a bash shell is opened.
With the alias in place, you can open a new shell instance (or terminal window) and run
Or in the same shell instance
Hi! Please help us improve the course!
Please consider the following statements and questions regarding this part of the course. We use your answers for improving the course.
I can see how the assignments and materials fit in with what I am supposed to learn.
I find most of what I learned so far interesting.
I am certain that I can learn the taught skills and knowledge.
I find that I would benefit from having explicit deadlines.
I feel overwhelmed by the amount of work.
I try out the examples outlined in the materials.
I feel that the assignments are too difficult.
I feel that I've been systematic and organized in my studying.
How many hours (estimated with a precision of half an hour) did you spend reading the material and completing the assignments for this part? (use a dot as the decimal separator, e.g 8.5)
How would you improve the material or assignments?