This TryHackMe room provides an introduction to Rust programming language for developers who already know programming but don't have experience with low-level programming. The room covers Rust's key concepts, syntax, and features that make it unique compared to other programming languages.
What other language is Rust similar to in terms of performance?
Answer: C++
Rust offers similar performance characteristics to C++, with memory safety guarantees that C++ lacks. Both languages provide low-level control over system resources while maintaining modern programming paradigms.
What famous company switched from Go to Rust, mentioned in this task?
Answer: Discord
Discord migrated significant portions of their infrastructure from Go to Rust, citing better performance and memory safety as key factors in their decision.
Microsoft Security Centre reports what percentage of CVE's they assign are memory safety issues? Include the % sign.
Answer: 70%
According to Microsoft's security research, 70% of Common Vulnerabilities and Exposures (CVEs) are related to memory safety issues, highlighting the importance of Rust's memory safety features.
What is Rust's version of NPM or PyPi?
Answer: Cargo
Cargo is Rust's official package manager and build tool, similar to NPM for JavaScript or pip for Python. It handles everything from downloading dependencies to building and testing Rust code.
What is the tool we used to install Rust called?
Answer: Rustup
Rustup is the official Rust toolchain installer and version manager. It allows you to install and manage multiple versions of Rust and its components.
How do we install the package rustscan using cargo?
Answer: cargo install rustscan
Cargo's install command downloads and compiles Rust binaries from crates.io, making it easy to install command-line tools written in Rust.
What command do we run to format our code?
Answer: cargo fmt
Rust comes with an official code formatter called rustfmt. The cargo fmt command automatically formats your Rust code according to standard conventions.
How do we initialise a new Rust project?
Answer: cargo init
The cargo init command creates a new Rust project in the current directory, setting up the project structure with a Cargo.toml file and src/ directory.
What does every Rust project need as a file?
Answer: main.rs
Every Rust project needs a main.rs file in the src/ directory, which serves as the entry point and contains the main function where execution begins.
If we wanted to add a dependency to our Rust project, what file would we edit?
Answer: cargo.toml
The Cargo.toml file contains project metadata and dependencies. Dependencies are added to the [dependencies] section with version specifications.
How do we run our Rust project?
Answer: cargo run
The cargo run command compiles the project if necessary and executes the resulting binary, making it convenient for development workflows.
How do we build the project RustScan with the release profile (most optimised)?
Answer: cargo build --release
The --release flag enables optimizations for production binaries, resulting in faster execution but longer compilation times.
What folder are the release binaries stored in?
Answer: target/release/
Rust organizes build artifacts in the target/ directory, with release builds stored in target/release/ and debug builds in target/debug/.
How many release profiles does Rust have using optimisation level?
Answer: 4
Rust has four predefined optimization levels: 0 (no optimizations), 1 (basic optimizations), 2 (more extensive optimizations), and 3 (aggressive optimizations).
In question 1, does this code compile? T(rue) or F(alse)
Answer: F
What is the error code returned by question 1?
Answer: E0308
Does the code in question 2 compile? T(rue) or F(alse)
Answer: F
What is the error message returned?
Answer: cannot assign twice to immutable variable
How do we define a constant in Rust?
Answer: const
Can we shadow a constant? T(rue) or F(alse)
Answer: F
What do we use to change the type of an immutable variable once it has been defined?
Answer: shadowed
Will the code "CONST word = "yes"" compile? T(rue) or F(alse)
Answer: F
Rust has a sophisticated system for variable mutability and constants. Variables are immutable by default, and mutation must be explicitly declared with mut. Constants must be uppercase and are always immutable.
We have "let word = "hello"", how do we get the length of the variable?
Answer: word.len();
Given the number -6, is this signed or unsigned?
Answer: Signed
Given the number 65536, what is the smallest unsigned datatype we can fit this into?
Answer: u32
What's the smallest sized signed integer in rust?
Answer: i16
Create a mutable u32 variable called "tryhackme" and assign it the number 9
Answer: let mut tryhackme: u32 = 9;
What data type is used to represent a string slice?
Answer: &str
Let's say you had a variable, X. You wanted to typehint the variable as a string. What would you write? Include X in the variable but not the let or = parts.
Answer: x: String
Rust has two main string types: String (owned, growable) and &str (string slice, borrowed). The language uses explicit typing with a focus on memory safety and performance.
Will question 1 return 8172192? T(rue) or F(alse)
Answer: F
Will example 2 run? T(rue) or F(alse)
Answer: F
What type should we give to the argument for question 3?
Answer: &str
The last expression in a function (the return) needs to have a semicolon. T(rue) or F(alse)
Answer: F
Every function need to return something. T(rue) or F(alse)
Answer: F
Functions in Rust can be nested within other functions. T(rue) or F(alse)
Answer: T
What keyword do we use to return early from a function?
Answer: return
You nest a function named main, inside another function named main. Will this run? T(rue) or F(alse)
Answer: T
Rust functions can be nested within other functions, allowing for flexible code organization. Function returns can be implicit (last expression) or explicit with the return keyword.
How do we break out of a loop?
Answer: break
Simplest keyword to make an infinite loop?
Answer: loop
Turn let a = [10, 20]; into something we can iterate over.
Answer: a.iter()
While loops can also be infinite. T(rue) or F(alse).
Answer: T
Iterators are lazy. T(rue) or F(alse).
Answer: T
For loops are explicitly mentioned in the Rust book as zero cost abstractions. T(rue) or F(alse).
Answer: F
Zero Cost Abstractions are common in high level languages like Python or JavaScript T(rue) or F(alse).
Answer: F
What crate do we use to easily make an iter multi threaded?
Answer: Rayon
How do we tell Rust to include an external crate into our program? What file do we place this information in?
Answer: cargo.toml
Turn a.iter() into a multi threaded parallel iter using Rayon
Answer: a.par_iter()
What website do we go to for Crates?
Answer: crates.io
Rust's iterator system is lazy by default, meaning operations aren't performed until explicitly consumed. The Rayon crate enables easy parallel iteration, demonstrating Rust's performance capabilities.
We can assign variables based on an if statement on one line T(rue) or F(alse)
Answer: T
What is the data type returned from opening a file?
Answer: Result
Write the datatype of a generic Result with type hints
Answer: Result
We're in a function and we get given a Result enum. If the Result is okay we want to continue working on it in this function. If the result is Err we want to return to the parent function with Err. What should we use?
Answer: ?
We're certain our result will always return Ok, what should we use?
Answer: unwrap
Rust's Result
After completing all the challenges and understanding Rust's core concepts, we capture the flag:
Flag: THM{Rust}
Rust prevents common memory-related bugs through its ownership system, borrowing rules, and lifetimes - all enforced at compile time.
Rust provides C/C++-level performance while maintaining high-level programming ergonomics and safety guarantees.
Excellent developer experience with Cargo package management, integrated testing, documentation generation, and automatic formatting.
Rust's type system prevents data races at compile time, making concurrent programming safer and more intuitive.