Introduction To Rust Programming
History of Rust

Rust was first released in July 7, 2010 but began in 2006 as a personal project by Graydon Hoare who at the time was a Mozilla employee. In 2009 Mozilla threw its support behind Rust and with there help Rust got its own compiler called “rustc” and compiled itself in 2011 with the first release of Rust using the new compiler releasing in early 2012 in alpha and stable release happening a few years later in 2015. and since then Rust has continued to evolve and become available on almost every OS and platform. A major milestone for Rust came in February 8, 2021 when Google, Microsoft, Huawei, Mozilla and Amazon all came together and created the “Rust Foundation” whose purpose is to support the development and maintenance of Rust.

Cargo

Cargo is the build in package manager and build system for Rust meaning cargo can very easily simply and organize Rust projects by building code, downloading crates (packages) from online and building those crates all with a single line. If you’ve every used NPM (node package manager) cargo is very similar to that but for Rust projects. There is a website called crates.io which Cargo uses to download crates that you can specify in a file called cargo.toml, this file also contains information about your project such as project name, project version and Rust version.

Why Rust

Rust is a low-level memory safe systems programming language that fits into the same category as C and C++. What makes Rust stand out over its competition is the fact that Rust is memory safe by design meaning there is no “NULL” and you will never run into a “null pointer” issue. Rust has no garbage collector but it is smart enough to manage memory so you never have to free memory, what all this means is that Rust is fast and does not need a garbage collector to free memory.

Memory safety issues account for a large percentage of all vulnerabilities in software and these issues cost developers time and money to fix but by using Rust, developers never have to worry about memory problems and instead can focus their time on more pressing matters.

Rust offers zero cost abstraction, which means Rust will make sure that abstraction is very performance efficient. Rust removes all runtime dependencies by default using a static build meaning you only need the executable to run a Rust program and no other files are required.

Rust has its own package manager called “Cargo” and it allows you to handle all your dependence, download libraries, build your code and those libraries all with a simple command. For this blog we will be installing Rust along with Cargo using Rustup.

Rust Install

Cargo Commands Break Down

There are lots of cargo commands but for this blog and the videos we will be focusing on the three commands explained bellow.

cargo new “projectname” — this will create a new Rust project, remove the quotes

cargo check — checks that the project will run but will not build the project

cargo build — will build the project and create an executable if there are no errors

cargo run — will build and run the project if there are no errors

More Info On Rust

Now that we have Rust installed and we have build a simple project using cargo it’s time to get into the basics of Rust. Unlike Java, Rust is not object oriented but Rust does have structs, impl, traits that can be used to somewhat mimic an object oriented environment. Structs can be used to group functions and properties. Impl are used to implement a struct by adding methods to it.Traits can be used to create abstract behaviors similar to an interface in Java.

The main function is the heart of a Rust program and it is the first thing that is run by the Rust compiler, all other functions must be called by the main function or be called by a function that is being called by the main function to run.

we create a function using the “fn” keyword and the word “main” makes this function the main function that must exist in all Rust programs.

Frequently Used Data Types In Rust:

The Rust compiler is able to determine if something is a string or a number or a float so we don’t have to explicitly specify the type for the most part but it is still good to know what types Rust provides by default and how to use them.

Signed (positive/negative number) i8, i16, i32, i64, i128, f32, f64, isize

Unsigned (positive numbers only) u8, u16, u32, u64, u128, usize

(usize, isize)— size of the architecture, so 32 bit machine this will mean u32

There is also a char (‘a’) and a boolean Boolean (True or false) along with &str for string slices and String for dynamic strings.

The Basics of Rust

Here i have made a video going over all the basics of Rust. This video covers things like how to print to the screen, variables, arrays, tuples,mutability, logic, etc. I also cover Rust specify things just as shadowing which allows you to re-declare a variable with a new value or new type.

Data Ownership and Borrowing

The barrow checker is responsible managing ownership of borrowed variables and determining when to return ownership of data to their correct owner. This video i made covers most aspects of borrowing in Rust and data transfer.

Loops

There are a few different ways to make loops in Rust and in this video we go over them and detail their differences and where each would be useful. Something to keep in mind is that the for loop in Rust is not index based, it is an iterator meaning that it is position based.

Structs, Impls and Traits

Rust is not an object oriented language but it does provide features like structs that can work similar to classes with impls providing functions and traits extending on the structs to create even more functionality and add a layer of abstraction.

Enums

An enum is kinda like a struct except it contains only values and can only be one thing at a time, a good use of an enum would be to hold things like error messages, colors, and things that can only be one thing at a time.

creating an enum

how to access element of enum

Error Handling

In Rust there is no exceptions for handling errors instead there is the Result<T, E> which returns Ok(T) for success and Err(E) for failure.

At line 140 we make a mutable string dynamic variable with the new method, at line 141 we are getting input from the console and putting that input into the line variable we made, we have a match against the value added to line by the read_line function of the io struct. The read_line returns a Result<Ok,Err> like all things in Rust, the Ok means that it worked, we got input and there was no errors, the Err will captures any error and put it in the variable E.

Creating a Calculator in Rust

Now that we have a solid understanding of Rust, lets create something that will incorporate many of the concepts we learned into a fun but simple first project. We also touch on modules and how to use connect Rust code from different files into one file.

Conclusion

Rust is not a easy programming language by any means, there is a step hill to climb when learning the language especially if you’ve only experienced Object Oriented Programming. Like all things if you keep at it, you’ll see that Rust is a very enjoyable and capable language to work with. We learned everything we needed from Rust to make a game in the next blog.

Final Code

Calculator Code: https://github.com/HereshAhmadi/RustBlog1TutoialCode/tree/main/Calculator

Tutorial Code: https://github.com/HereshAhmadi/RustBlog1TutoialCode/tree/main/TutorialCode

References

https://doc.rust-lang.org/book/

https://en.wikipedia.org/wiki/Rust_(programming_language)

https://www.ralfj.de/projects/rust-101/main.html

https://doc.rust-lang.org/stable/rust-by-example/
['Heresh Ahmadi']
['Programming', 'Tutorial', 'Rust']