Day 1/30 - Learning Rust 🦀

Day 1/30 - Learning Rust 🦀

Introducing the 30-Day Rust Coding Challenge! 🦀 This challenge will go into the realm of Rust programming. We shall choose a topic related to Rusts every day and thoroughly study it. The official Rust documentation will cover every single topic that we will study throughout this challenge.

Today's topics explored include:

  • Rust Installation

  • Rust First Program (Namaste, Rust!!!)

  • How to build projects with Cargo

1. Rust Installation

Before diving into Rust, let’s set up our development environment. Follow these steps to install Rust:

  1. Windows:

    • Visit the official Rust website.

    • Download the installer and run it.

    • Follow the installation instructions.

  2. Linux/macOS:

    • Open your terminal.

    • Run the following command:

        curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
      
    • Follow the prompts to complete the installation.

  3. Verify the installation by running:

     rustc --version
    
💡
A local copy of the documentation is also included with the Rust installation so you may read it offline. To access the local documentation in your browser, run command: rustup doc
😊
You can update your Rust current version to latest version without downloading it again: $ rustup update
🥲
You can also uninstall Rust too: $ rustup self uninstall

2. Rust First Program (Namaste, Rust!!!)

Let’s create a simple “Namaste, Rust!” program. Open your favorite text editor (VS Code 😅) or IDE and create a new file named hello.rs. Add the following code:

// hello.rs
fn main() {
    println!("Namaste, Rust!");
}

Save the file and open your terminal. Navigate to the directory where hello.rs is located and run:

rustc hello.rs
./hello

You should see the output: Hello, World!

Rust macro
println! calls a Rust macro. If it had called a function instead, it would be entered as println (without the !). You just need to know that using a ! means that you’re calling a macro instead of a normal function and that macros don’t always follow the same rules as functions.

🚨
You will also need a linker, which is a program that Rust uses to join its compiled outputs into one file. It is likely you already have one. If you get linker errors, you should install a C compiler, which will typically include a linker. A C compiler is also useful because some common Rust packages depend on C code and will need a C compiler.
👉
To install C compiler on Linux: sudo apt install build-essential
💡
Use the automatic formatter tool rustfmt to format your code in a specific manner if you want to maintain consistency across all of your Rust projects. This tool is already installed on your machine because the Rust team includes it with the standard Rust distribution, which is rustc!

3. How to build projects with Cargo

What is Cargo?

Cargo is the package manager and build system for Rust. It is essential to the Rust ecosystem because it facilitates code compilation, dependency management, and package publishing. Cargo is installed automatically when you use rustup to install Rust.

How Cargo Works

  1. Cargo.toml: This file acts as your project's manifest. It includes important details like dependencies, version, and project name. Here is where you declare your dependencies; Cargo will take care of the rest.

  2. Cargo.lock: This file's goal is to guarantee a reproducible build. It keeps track of the precise dependency versions that were utilized in the most recent successful build. By sharing your project, you help others avoid surprises by allowing them to utilize the same versions.

Cargo Commands

Here are some common Cargo commands:

  • Creating a Project with Cargo:

      $ cargo new hello_cargo
      $ cd hello_cargo
    
  • Building and Running a Cargo Project:

      $ cargo build
         Compiling hello_cargo v0.1.0 (file:///projects/hello_cargo)
          Finished dev [unoptimized + debuginfo] target(s) in 2.85 secs
    
      $ ./target/debug/hello_cargo # or .\target\debug\hello_cargo.exe on Windows
      Hello, world!
    
  • Running a Cargo Project (One-Linear/Easier way to run):

      $ cargo run
          Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
           Running `target/debug/hello_cargo`
      Hello, world!
    
  • Cargo has a command known as cargo check. This tool generates no executable, but it quickly verifies that your code compiles.

      $ cargo check
         Checking hello_cargo v0.1.0 (file:///projects/hello_cargo)
          Finished dev [unoptimized + debuginfo] target(s) in 0.32 secs
    

I'm very grateful that you read! 🤧

I hope you found this blog post to be useful.

Happy coding! 😊🦀

Did you find this article valuable?

Support Divyansh Mehta by becoming a sponsor. Any amount is appreciated!