Introduction
Learning objectives
- You know what kind of a programming language Rust is on a high level and what it is used for.
- You know the history of programming languages that has led to Rust.
- You know how to create and run a Rust application on your computer.
Hello rustaceans!
This module introduces you to the basics of the Rust programming language (and beyond). Similar to many programming courses, we start with a program that outputs a Hello world!
-like message. You can run the program by pressing the Play
-icon .
When you run the above program, you see the message Hello rustaceans! 🦀
. Below, you have the first programming exercise. You can open the programming exercise by clicking the title.
Let's start by solving a simple exercise to get an initial feel for Rust and solving programming exercises on this course platform.
You can complete the exercises in this course either using the embedded editor on the website that opens with the exercise, or you can use your own editor on your own computer to write the code and upload the file containing the code. You can also click the button in the embedded editor to open the Rust Playground in a new tab. More on running Rust programs on your own computer later in the section Running Rust on Your Computer.
Prerequisites
Although we started with a very simple exercise, our expectation is that you already have some prior programming experience. To comfortably proceed with the course, you should at least know how to work with variables, conditional statements, loops, functions, and basic data structures. Knowledge about classes and objects (or object-oriented programming) helps you in the course as well.
In addition, you should already be familiar with using programming environments, have some familiarity with working on the command line, and to some extent understand that some programming languages are compiled by a compiler before they can be run.
Registration
Registration happens by creating an account for this platform. Creating an account can be done through the REGISTER link on the upper right-hand corner of this page. If you have previously worked on the FITech 101: Digi & Data -course series, currently only in Finnish, the same account can be used here. When you work as a registered user, your progress will be stored.
It is possible to also work on some of the course assignments without registration. If you choose to continue without registration, your progress may disappear if you e.g. clear your cookies, do not visit the materials for a while, or if we do cookie-clearing changes to the course platform.
Grading
The course is worth 3 ECTS credits, which corresponds to roughly 80 study hours. The amount of study hours naturally varies between individuals. By completing at least 90% of the course exercises, you can apply for the credits by following the guidelines on the Applying for credits page. Note that for official Aalto credits, a Finnish social security number is required.
Working practices
This is a self-study course without explicit deadlines. We reserve the right to update the course materials (and remove or add assignments) at any time for the purposes of improving the course.
When reading the materials, try out all the examples in the materials and search for more information online. In this course, learning about Rust goes hand in hand with learning to write Rust. When you try out the examples, you will observe what works and what does not work.
The course materials include quizzes and programming assignments that are interspersed throughout the materials. Trying out the examples help you in solving the programming assignments. In addition to the quizzes and programming assignments.
All the assignments are completed individually. Sharing your solutions to others is strictly forbidden.
Question not found or loading of the question is still in progress.
As the assignments have been created to help everyone learn, do not take away that opportunity. Do not share your solutions to the assignments or store them in any publicly available location.
All submitted coursework is also periodically checked for plagiarism. Plagiarism, i.e. representing the work of others as your work, will naturally lead to a failed course, and possible other ramifications.
The use of generative AI and large language models such as ChatGPT for completing coursework is not allowed, unless specifically allowed for a given assignment in the assignment handout. Using them relates to representing the work of others as your own.
Question not found or loading of the question is still in progress.
What is Rust and why Rust?
Rust is a general-purpose programming language used for both systems programming and application programming. Rust is designed to be a highly performant and safe language with a special focus on safe parallel computing; it offers an equally fast alternative for popular older systems programming languages like C or C++ which are used to create systems software such as operating systems (e.g. Linux, Windows), device firmware or game engines. Rust began as a personal project by Graydon Hoare in 2006 and was officially sponsored by Mozilla in 2009. Rust first appeared in 2010 and the first stable version was released in 2014. Today, Rust is backed by the non-profit membership organization Rust Foundation. During the years, Rust has brought together a rich and active community developing useful libraries for all kinds of applications.
Winner!
Rust has been selected the most loved programming language in StackOverflow's yearly survey for seven years in a row now. We are not trying to bias you, but it is awesome.
Key feature: memory safety
An important aspect in Rust is memory safety, which prevents programmers from introducing memory-related bugs and security vulnerabilities in code. This is achieved in Rust with a borrow checker, which automatically ensures memory safety at compile-time by verifying that references live only as long as the data they are referencing while also preventing data races from happening. This is a big advantage when compared to programming languages with similar performance such as C and C++ that do not feature memory safety and have programmers manually allocate and free memory.
Consider the following example where we count the number of words in a string.
In Rust, ampersands (&
) before literals or variables denote borrowing aka referencing a value instead of taking ownership of the value. If we modify the code by removing the ampersands, the code will not work due to Rust's ownership rules enforced by the borrow checker.
Note that memory safety through automatic memory management is not a novel feature in programming languages. Many programming languages have overcome manual memory management by implementing a garbage collector that during program runtime attempts to find and reclaim memory that was allocated but is no longer referenced by the program. Garbage collection, however, has its own downsides by requiring computing resources. In addition, with programming languages that rely on garbage collection, one has to -- or should -- pay attention as to not introduce memory leaks through e.g. sloppy coding.
Understanding Rust's borrow checker takes a bit of effort, as one needs to understand the ownership paradigm to understand the principles of the borrow checker. Luckily, and to aid with this, Rust's developers have done a great job in creating a compiler that provides informative error messages that often help the typical programmer who unwittingly includes mistakes in their code. We will take a closer look at ownership and the borrow checker in the next section when we write our first functions in Rust.
Before Rust, two main approaches were used manage memory in programming languages: manual memory management within code, and garbage collection.
Manual memory management gives control to programmer at the expense of having to be careful not cause memory-related bugs. Memory safety is solely in the hands of the programmer.
Garbage collection in comparison makes life easier for developers by providing memory safety at the cost of resources and performance. Memory is automatically allocated when needed and a garbage collector automatically reclaims memory allocated by the program that is no longer needed (garbage) during the program runtime.
Rust takes a third approach, ownership, that is able to guarantee memory safety efficiently without the risk that comes with manual memory management. The following table summarizes the three approaches.
Approach | Pros | Cons | Example languages |
---|---|---|---|
Manual memory allocation and deallocation | Developer is in control | Difficult to get right | C, C++, Zig |
Garbage collection | No need to worry about memory safety, faster (memory safe) development | Runtime costs, unpredictable performance | Java, Python, JavaScript, Erlang |
Borrow checker | Best of both worlds | Need to abide by strict compiler rules | Rust |
Syntactic similarities
The syntax of Rust is in many ways similar to that of C and C++ (and a bunch of other languages like Java and C# that are considered a part of the "C-family" of programming languages). We can notice this in the example programs below that print the first 10 numbers in the Fibonacci sequence -- sequence of sums of previous two values that starts from 0 and 1.
You don't need to worry about the intricacies of the code (at least not yet). The samples are just for syntax comparison.
Fibonacci in C++
Fibonacci in Rust
Interoperability and adoption
Presently, the older languages C (1972) and C++ (1985) remain dominant in many areas where the newer Rust could be considered a better alternative. To make adoption of Rust easier in existing projects, Rust allows existing C/C++ programs to be easily extended or partly replaced using Rust code via its Foreign Function Interface (FFI).
C and C++ are also found in multiple libraries of the popular language Python (the third-most popular programming language according to TIOBE) to allow for faster computation than is possible with pure Python. Perhaps unsurprisingly, there already exist tools which allow creating Python libraries in Rust easily. If you are interested, see for example PyO3 and some examples of Python libraries written in Rust. Rust can also run on websites in a browser as an alternative to the dominating browser programming language JavaScript by compiling Rust to WebAssembly.
Did we mention that Rust is awesome?
We might have mentioned that Rust is awesome, but just in case you missed it, we'll reiterate. Rust is awesome 🦀. It has a plenty of upsides, especially for systems programming, but it also has some features that one might consider downsides. We have created a synthesis of these here.
Upsides
- Rust is performant
- Zero cost abstractions make efficient code quick and easy to write
- Compiled programs execute fast
- No garbage collector
- The only checks during runtime are for index out of bounds
- Ergonomic error handling
- Recoverable errors are handled using types
- Parts of the program that can fail are easy to spot
- Rust is (memory) safe
- Rust compiler ensures no memory bugs can happen during runtime
- Unless explicitly using
unsafe
code for advanced low-level operations
- Unless explicitly using
- Safe and efficient concurrency
- Rust compiler ensures no memory bugs can happen during runtime
- Has a vibrant community that pushes the language and its utilities forward
- An extensive standard library
- Many useful libraries
- Highly integrated package manager
- Helpful compiler
- Not only complains about your mistakes, but suggests fixes
Downsides
- Rust is verbose
- Some basic operations require more code than many other languages
- Compilation times can be slow
- Writing code can often be slower than in many other contemporary languages
- The programmer needs to work with a very strict rule set enforced by the compiler
- Requires learning a unique programming paradigm
Course authors
The course platform has been created by Arto Hellas, Sami Sarsa, and Jesper Pettersson from the Aalto University. The platform has been initially created for the FITech 101: Digi & Data course series (currently only in Finnish).
The materials and assignments for the course have been written by Sami Sarsa, Niklas Halonen (sponsored by Eficode) and Arto Hellas. A huge thank you goes to all the course participants, as creating a course is always dependent on the participants and their feedback.
The platform relies on a number of technologies, which include Docker, GatsbyJS, React, PostgreSQL, JavaScript and TypeScript, Dart and Dartpad, Ace editor, Deno, NodeJS, Express, Nightwatch.js, Cypress, Flyway, and a rather large number of individual libraries.
A huge shoutout to those responsible for the underlying work that has made this course possible, especially the Rust community and Rust Foundation. We would also like to thank the following people for their feedback and contributions to the course: Matias Zwinger, Henrik Lievonen, Roope Salmi, Dennis Marttinen, Verneri Hirvonen, Lucas Käldström. Thank you!
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?