DEV Community

RealACJoshua
RealACJoshua

Posted on

🚀 Rust Basics 1: Getting Started with Rust Programming 🦀

Welcome to RUST BASICS 1, the first post in our 7-part journey to mastering Rust! Whether you’re a beginner or just curious about Rust, this series will guide you step by step.


Step 1: Install Rust

Let’s set up Rust on your computer.

For Linux/Mac/Windows (WSL):

  1. Open your terminal or command prompt.

  2. Run this command:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Enter fullscreen mode Exit fullscreen mode

Follow the on-screen instructions. After installation, verify Rust is installed by typing:

rustc --version
Enter fullscreen mode Exit fullscreen mode

This should display the installed Rust version.

Alternative Installation Methods

macOS: Use Homebrew:

brew install rust
Enter fullscreen mode Exit fullscreen mode

Windows: Download the Rust installer from Rust Official.


Step 2: Write and Run Your First Rust Program

Let's write the classic "Hello, world!" program.

  1. Create a folder for your Rust projects:
mkdir rust_projects
cd rust_projects
Enter fullscreen mode Exit fullscreen mode
  1. Inside the folder, create a file named main.rs.

  2. Open it in your favorite text editor (e.g., VS Code).

  3. Write the following code:

fn main() {
    println!("Hello, world!");
}
Enter fullscreen mode Exit fullscreen mode

fn means function.

main() is the entry point of a Rust program.

println! prints to the console. The ! indicates it's a macro.

  1. Save the file and run it:
rustc main.rs
./main
Enter fullscreen mode Exit fullscreen mode

The output should be:

Hello, world!


Step 3: Variables in Rust

Variables in Rust are immutable by default, but you can make them mutable with the mut keyword.

Update your main.rs file with this code:

fn main() {
    let name = "TheACJ";
    let age = 25;
    let mut score = 100; // `mut` makes the variable mutable

    println!("Name: {}", name);
    println!("Age: {}", age);
    println!("Score: {}", score);

}
Enter fullscreen mode Exit fullscreen mode

Explanation:

let name = "TheACJ"; creates an immutable variable.

let mut score = 100; creates a mutable variable.

{} is a placeholder in println!, replaced with the variable's value.

Add this to your code:

    score = 120; // Reassigning the value of score
    println!("Updated Score: {}", score);
Enter fullscreen mode Exit fullscreen mode

Run it again, and you’ll see the updated score.(This shows the power of mut)


Step 4: Taking User Input

Let’s make the program interactive by asking for user input.

Replace the code in main.rs with:

use std::io;

fn main() {
    let mut favorite_color = String::new();
    println!("Enter your favorite color: ");

    io::stdin()
        .read_line(&mut favorite_color)
        .expect("Failed to read input");

    println!("Your favorite color is {}", favorite_color.trim());
}
Enter fullscreen mode Exit fullscreen mode

Explanation:

use std::io; imports Rust’s input/output library.

String::new() creates a new, empty string.

read_line reads user input and stores it in favorite_color.

.trim() removes extra spaces or newline characters from the input.

Run it and interact with your program!


Step 5: Practice Challenges

A. Modify the program to ask for your age and print a message like:

You are 25 years old!

B. Write a program to calculate the square of a number:

let num = 4;
println!("The square of {} is {}", num, num * num);
Enter fullscreen mode Exit fullscreen mode

Feel free to share your solutions in the comments for feedback!


Troubleshooting Tips

If rustc isn’t recognized, ensure Rust is added to your PATH. Restart your terminal or run:

source $HOME/.cargo/env

If your program doesn’t work as expected, double-check for typos.


🎉 That’s it for RUST BASICS 1! Congratulations on taking your first steps in Rust programming. Stay tuned for RUST BASICS 2, where we’ll dive deeper into more advanced concepts.


Let me know if you found this helpful or if you ran into any issues. Happy coding! 🦀

Feel free to look at Rust Doc

Top comments (0)