Task 1: What is Rust?
Found an issue in this room? Leave a message in Discord and someone will tag me if I don't see it :)
Reminder This is a walk through room with no extra points for first bloods. Please take your time, copy the copy and experiment with it.
The attached file is this entire room, but as a PDF. If you are having accessibility issues with this room, refer to the PDF which may be much better interpreted.
The Crab is Rust's mascot and is taken from here. All other images are taken from Undraw.
Introduction
Rust is a new programming language created in 2015 by a small team of people, and later adopted by Mozilla (the organisation that created & maintains Firefox).
It is a compiled low level language, which aims (and succeeds) to be the same speed as C++, but while incorporating some higher level language features from fan-favorites such as Python or JavaScript.
Rust has 3 goals:
- Fast
- Secure
- Productive
Fast
Rust aims to be similar in terms of performance to C++.
Rust is statically typed, which means the data type of a variable is known at compile time. This allows the compiler to optimise the code further than if we didn't know the types.
Rust does not use garbage collection (despite being a low level programming language). Garbage collection is where the program attempts to reclaim memory from garbage. Garbage is memory occupied by objects that are no longer in use by the program.
Go, a high level programming language similar syntactically to Python but is fast & compiled, uses garbage collection. This caused a massive overhead at Discord, which forced them to switch from Go to Rust. https://blog.discord.com/why-discord-is-switching-from-go-to-rust-a190bbca2b1f
Something to note is that Python and JavaScript use garbage collection. These abstractions may cause issues (as in Discord's case), which is why many choose a low level programming language.
Secure
Rust is completely memory safe. This means that exploits involving memory aren't possible in Rust, unless you explicitly specify unsafe Rust code.
The Microsoft Security Response Centre states that 70% of all CVE's MSRC assigns are memory safety issues. In Microsoft's own words:
"This means that if that software had been written in Rust, 70% of these security issues would most likely have been eliminated. And we’re not the only company to have reported such findings."
Sometimes programmers must perform unsafe operations. Rust provides tools to wrap these unsafe actions so unsafe code can be statically enforced by the Rust compiler.
The memory safety is guaranteed by the concept of ownership. All Rust code follows these rules:
- Each value has a variable, called an owner.
- There can only be one owner at a time.
- When the owner goes out of scope, the value will be dropped.
Values can be moved or borrowed between variables, but no value can have more than 1 owner.
Let's see an example of Python failing with this:
squares = (val * val for val in range(100))
print(min(squares))
print(max(squares))
What we want is:
0
9801
>>> print(min(squares))
0
>>> print(max(squares))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: max() arg is an empty sequence
In Rust, the same code is:
fn main()
let squares = (0..100).map(|val| val * val);
println!("{:?}", squares.min());
println!("{:?}", squares.max());
}
When we try to compile this, Rust tells us:
error[E0382]: use of moved value: `squares`
--> ownership.rs:4:21
|
3 | println!("{:?}", squares.min());
| ------- value moved here
4 | println!("{:?}", squares.max());
| ^^^^^^^ value used here after move
|
= note: move occurs because `squares` has type `std::iter::Map<std::ops::Range<i32>, [closure@ownership.rs:2:31: 2:40]>`, which does not implement the `Copy` trait
I'll talk more about this in a later task, but the important part is that Python allows functions to alter variables they do not own, whereas Rust doesn't.
PS: Notice how the Rust compiler explicitly points out the values, the lines, the exact characters, where the error occurred as well as a full error message explaining why it won't allow that code. Whereas Python simply said max() arg is an empty sequence.
Productivity
Rust's 3rd largest goal is a strange one. Productivity!
Rust provides all of the tools developers need to be productive, shipped with the platform itself.
Note: The below list is read as:
- Tool
Explanation of the above tool.
Some of these include:
- Cargo
Rust's version of NPM or PyPi. Download packages others have created.
- Clippy
Microsoft Clippy, but re-imagined for Rust to aid with development.
- RustFmt
Automatically formats Rust code
- Cargo Test
A built in testing application created by the Rust developers.
- Cargo docs
Automatically generate documentation for your code, using documentation comments (written in Markdown). This documentation is then sent to docs.rs upon publishing to Cargo. Not to mention that examples written in documentation are automatically tested for you. No more untested documentation examples!
- Rust-Analyzer
Think IDE but more intelligent. Rust Analyzer clearly labels what is wrong with your code, why it is wrong, the exact characters that conflict and cause the error, and 90% of the time it provides an "auto-fix" function that automatically fixes these errors for you.
- The Rust Book & Docs
Rust has a book, called The Book which details everything you could want to know about Rust. Neatly chaptered, easily searchable and at your disposal for free. If this isn't good enough, thanks to Rust's documentation comments almost every library you'll use will have extensive documentation online.
With all of these tools at your disposal, it is incredibly rare to compile a Rust program and have bugs in it. In fact, I have only experienced this once. 99% of the time, the tooling and language will have picked up on it long before I hit compile.
Conclusion
If you are looking for something extremely fast and memory safe but while maintaining good productivity, Rust is the language for you.
As Pentesters our job is offering solutions to developers. Telling Python developers that a low-level language is a good alternative sounds wacky at first.
But Rust can hold your hand, as it supports calls from functions written in other languages (foreign function interfacing).
We can use Rust to rewrite security or performance critical code which will cooperate with our existing codebase.
Here's an example of calling C code extern "C" {
fn abs(input: i32) -> i32;
}
fn main() {
unsafe {
println!("C believes that the absolute value of -3 is: {}", abs(-3));
}
}
Answer the questions below
- What other language is Rust similar to in terms of performance?
What famous company switched from Go to Rust, mentioned in this task?
Microsoft Security Centre reports what percentage of CVE's they assign are memory safety issues? Include the % sign.
What is Rust's version of NPM or PyPi?


It wouldn't be a programming tutorial without a basic "Hello, World!".
Rust also has constants. These are values that aren't just immutable by default, but are always immutable.
In Rust we'll very often see the compiler complain that our variables and functions aren't type hinted. We saw type hints with 



Now go into cargo.toml, and include it in the dependencies section. Just copy and paste it across.
If statements work exactly like how you'd expect.
Now we get into the inner-workings of Rust, the thing that helps keep it safe. Error handling.