Jon Dean’s
Hello, World! <Rust: &mac_edition>
Ferris says this tutorial is for Mac Catalina 10.15.4
A Guide to the installation of Rust. Followed by a Guide for a simple “Hello, World!” Rust Application.
What is Rust?
Well, Rust is an Open Source Memory-Safe Language made for those who value Speed and Stability. Rust was made by Graydon Hoare at Mozilla Research, with each newer version being run by a large and wonderful open-source Community.
Why use Rust over another Language?
Unlike other languages, Rust comes with a built-in Compiler made in well, Rust. The compiler does checks for execution errors, unsafe code, uncompilable code, and a majority of other safety checks that help produce an environment that embodies the principles of safe, and fast code.
No more are the days of Lemon Logic and Spaghetti Binary, hark the age of Ferris and his Snipping of memory leaks.
With our intro out of the way, let’s begin!
Step 1: Installing Rust and getting it Ready
- Run this script
curl https://sh.rustup.rs -sSf | sh
in your terminal and follow the default installation instructions. If it installs you should see:
- After Run
rustup update
in order to update to the latest version - Finally Run
rustc --version
to make sure you’re up to date
Current Version at time of article is: 1.44.1
Check at: https://www.rust-lang.org/
Step 2: Start up your Project Workspace
- cd to a directory you like
cd ~/Documents
- Next create your projects folder and cd to that folder
mkdir projects | cd ./projects
Step 3a: Hello World (Without Cargo) Setup
- Inside of your projects folder let’s create a new directory named
hello_world
, a new directorysrc
inside of./hello_world
and a file inside of./hello_world/src
namedhello_world.rs
.
mkdir hello_world
cd ./hello_world
mkdir src | touch src/hello_world.rs
- Next open up the newly created file
src/hello_world.rs
and type the following function:
When creating a function in Rust you must take into account 4 factors,
1. fn: The fn keyword is the start of your function declaration2. main: The name of the function, (The main keyword is special as it is the first function to run in your application)3. (): The Parentheses signify that whatever is enclosed by them is a parameter, each one separated by a comma.4. {}: The Brackets enclose the code block that is scoped lexically to the appended function, each piece of code is separated by a “;”.
Next we need to write out a statement that will print some text to our console. Thankfully rust has a method for that println()
, so let’s write that inside of our main function.
Step 3b: Hello World (Without Cargo) Logic
- Rust Functions are not able to be supplied what is known as a
vararg
, or aVariable Argument
ex: fn main(FooBar) is incorrect
To combat this Rust usesFunction Abstraction
to create a Macro which when the keyword is followed by a bang, allows the use of varargsex: println!( “Foo Bar”) is correct
. println!()
, a macro, parses the format string at compile time into type safe code and logs the output to our console.- Inside of our
println()
, let’s supply the macro a string of,“Foo, Bar!”
.
Step 3c: Hello World (Without Cargo) Compile and run
- You may be used to just running the program however rust is special in that it compiles itself and performs memory safe operations during the compiling phase. Let’s compile our program by running
rustc src/hello_world.rs
.
- After we run this in our Root Working Directory folder we should see a new file created called
hello_world
. This is our compiled file and we must execute this file in order to run our application, if we do that we should get“Foo, Bar!”
Success! You have written your first program in Rust!
Optional: Hello World with Cargo
Replace step 3 with the Step 4
Step 4a: Hello World (With Cargo) Setup
Directory from: cargo new hello_world
- run
cargo new hello_world
, this creates a new application with a collection of files.
- cd to the directory inside of projects @location of
~/Documents/Projects/hello_world
Step 4b: Hello World (With Cargo) Compile and run
- Next type
cargo run
- If we check our logic inside of
/src/main.rs
; we can see that this logic matches what is happening inside of the console,A specified String is printed to the Console
.
Additional Reading:
https://doc.rust-lang.org/stable/rust-by-example/hello.html
https://doc.rust-lang.org/stable/rust-by-example/macros/variadics.html