Rust for starters (Part- 1)

Doga Budak
3 min readOct 9, 2022

--

When Gardon Hoare started the project in 2009, he probably did not think it would come this far. With the support of the Mozilla crew, Rust was introduced to the world in 2010. It was developed Until 2015 and finally released its original version. Until its 1.0.0 version, it was backed but over 900 open-source developers and the Mozilla team. But what is Rust for the rest of the world and what is the hype about?

Almost every browser we use today is written with C++. So imagine for firefox they wrote 12 million and for chrome ca. 5 million lines of code. You can already guess that this may cause some problems. Because thanks to C and C++ our browsers are fast, but because of memory manipulation they are not super reliable. If we don't write our code very carefully, the software can crash and Memory leaks, buffer overflow, or null pointer errors might occur. Even worse you might experience some security issues, and actually, this is the one that has the biggest consequences.

On the other hand, we have Haskell, it is very reliable and extremely sage but you basically you cant control memory allocation or other hardware resources. So you might think the more control we have over low-level, the less security we have. Rust is designed to overcome this problem.

Its strong typing mechanism provides us with high security and thanks to its design we also have control over low-level resources. Rust may provide us, developers, with a secure and fast environment.

Let's Code

How to install Rust and Cargo

Probably the easiest thing in this guide is to install Rust. You can also find it on the official website but let's leave it here for specification. And easiest way to install Rust and Cargo is using rustup. On Linux and macOS systems, this is done as follows:

curl https://sh.rustup.rs -sSf | sh

rustup downloads and installs The Rust Programming Language from the official release channels, allowing you to easily switch between stable, beta, and nightly compilers and keep them up to date. Rustup also provides quick access to the nightly compiler. You can run the following command to install the nightly build.

rustup toolchain install nightly

And following to switch between builds.

rustup default nightly

Our first program with Rust

Let's go of tradition and write our first program by creating a hello world program.

fn main() {
println!(“Hello World!”);
}

And let's save this as hello.rs. You can compile this code by writing rustc hello.rs. This compiled code will create an executable file in the same folder. We can execute it directly by writing ./hello. And you will see that it prints Hello World on the screen.

To explain to you what is happening briefly, you can actually already see that our first program looks a lot like Java or C++ code. Almost every other language rust also starts with a main() function. And the fn you see in front indicates that it's a function. The curly bracelets define the scope of this function. Rust uses " to define the strings and to write stuff to console, you should use println! macro. The exclamation mark is to specify the macro here. Then you finish your line with a ;. So many similarities with other languages right?

What is Cargo

We install cargo along with rust when we install it. What exactly is Cargo in Rust? Cargo is the build system and package manager for Rust. Because it allows Rust packages to declare their dependencies in the manifest, you’ll get a repeatable build with this tool., Cargo.toml.

How to install packages with Cargo

A crate can be installed from a variety of sources. The default location is crates.io but the --git, --path, and --registry flags can change this source. But in general, you can use the following command.

cargo install [options] crate[@version]

That was the beginning of a long series, more parts will come soon. In the next chapter, I will explain more keywords from Rust and we will write our first program.

For the other parts

Part 2: https://medium.com/@dogabudak/rust-for-starters-part-2-structs-34515bc89fdd

--

--