Post

Why Rust?

What is Rust?

Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safe.

Why Rust?

Rust is super fast compared to any other modern programming language. It is also very safe and has a lot of features that make it a great choice for systems programming. Rust is also very easy to learn and has a great community.

What are the features of Rust?

Rust has many features that make it a great choice for systems programming. Some of the features are:

  • Memory safety
  • Thread safety
  • Type safety
  • Zero-cost abstractions
  • Pattern matching
  • Generics
  • Traits
  • Type inference
  • Ownership
  • Borrowing
  • Lifetimes
  • Macros
  • Cargo

What are the advantages of Rust?

  • It is fast
  • It is safe
  • It is easy to learn
  • It has a great community
  • It has a great ecosystem
  • It has a great tooling
  • It has a great documentation
  • It has a great compiler
  • It has a great package manager
  • It has a great standard library

What are the disadvantages of Rust?

  • It is not as mature as other programming languages
  • It is not as popular as other programming languages
  • It is not as easy to learn as other programming languages
  • As it directly interacts with the system internals, it is not as safe as other programming languages

How to install Rust?

Rust can be installed using the rustup tool. The rustup tool can be installed using the following command:

On linux

1
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

On Windows

Native

Setup your machine for Rust development by following the instructions at Set up your dev environment on Windows for Rust).

Rust works with Windows; however, it is not officially supported by the Rust team. You can find more information about Rust on Windows at Rust on Windows.

If you plan to deploy and run your Rust application on Linux containers (which is a preferred choice) or machines, then you can use the Windows Subsystem for Linux (WSL) to install Rust. I prefer this appproach as it is not that difficult to setup and use and it is also easy to switch between Windows and Linux. Without additional effort or configuration, you can develop rust applications on Windows and deploy them on Linux.

On Windows with WSL

Open WSL command prompt in your windows machine, and run the following command:

1
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

We will discuss how to test your code developed in Windows on WSL later in the article.

Hello World in Rust

1
2
3
fn main() {
    println!("Hello, world!");
}

How to compile and run Rust code?

Rust code can be compiled and run using the rustc command. Then you can run the compiled code using the following command:

1
./hello_world

Cargo

Cargo is the package manager for Rust. It is used to manage dependencies and build your Rust projects. It is also used to run tests and generate documentation for your Rust projects.

How to create a new Rust project using Cargo?

You can create a new Rust project using the following command:

1
cargo new hello_world

How to build a Rust project using Cargo?

You can build a Rust project using the following command:

1
cargo build

How to run a Rust project using Cargo?

You can run a Rust project using the following command:

1
cargo run

How to add dependencies to a Rust project using Cargo?

You can add dependencies to a Rust project using the following command:

1
cargo add <dependency>

How to include dependencies in a Rust project using Cargo?

You can include dependencies in a Rust project using the following command:

Create new Rust Project

1
cargo new hello_world

Add dependencies

1
cargo add rand

Modify main.rs to include new dependency

1
2
3
4
5
6
use rand::Rng;

fn main() {
    let secret_number = rand::thread_rng().gen_range(1..101);
    println!("The secret number is: {}", secret_number);
}

Build and Run project

1
2
3
4
5
6
cargo run

Output: 
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/hello_world`
The secret number is: 42
This post is licensed under CC BY 4.0 by the author.