DEV Community

Cover image for πŸ¦€ Rust Series 1: From 0 to 1
Thor
Thor

Posted on • Edited on

πŸ¦€ Rust Series 1: From 0 to 1

Welcome to Rust! In this series, we'll cover the basics of Rust programming, starting from installation and setup to writing and running your first program. We'll also cover key data types, variables, and basic operations with examples and edge cases.


βœ… 1. Installing Rust

πŸ› οΈ Install Rust using rustup

rustup is the recommended way to install Rust as it manages updates and toolchains easily.

➑️ Steps to install:

  1. Open your terminal.
  2. Run the following command:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Enter fullscreen mode Exit fullscreen mode
  1. Follow the on-screen instructions.
    • It will install:
      • rustc – The Rust compiler
      • cargo – The Rust package manager
      • rustup – The version manager

βœ… Verify Installation

After installation, restart your terminal and run:

rustc --version
cargo --version
Enter fullscreen mode Exit fullscreen mode

Example output:

rustc 1.76.0 (f9e8851be 2025-03-01)
cargo 1.76.0 (f9e8851be 2025-03-01)
Enter fullscreen mode Exit fullscreen mode

πŸš€ 2. Setting Up Your First Rust Project

➑️ Create a new Rust project

Use cargo to create a new project:

cargo new hello-rust
Enter fullscreen mode Exit fullscreen mode

This creates a directory hello-rust with the following structure:

hello-rust
β”œβ”€β”€ Cargo.toml   // Project configuration file
└── src
    └── main.rs  // Main code file
Enter fullscreen mode Exit fullscreen mode

πŸ“ 3. Writing Your First Program

Open src/main.rs and write the following:

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

➑️ Run the program

Navigate to the project directory and run:

cargo run
Enter fullscreen mode Exit fullscreen mode

βœ… Output:

Hello, Rust!
Enter fullscreen mode Exit fullscreen mode

πŸ”’ 4. Numbers in Rust

Rust has strict type-checking for numeric types:

➑️ Signed Integers (i8, i16, i32, i64, i128)

  • Signed integers can store negative values.
  • i32 is the default type for integers.

Example:

let x: i32 = -10;
let y: i32 = 20;
println!("Signed Integers: x = {}, y = {}", x, y);
Enter fullscreen mode Exit fullscreen mode

➑️ Unsigned Integers (u8, u16, u32, u64, u128)

  • Unsigned integers store only positive values.

Example:

let x: u32 = 10;
let y: u32 = 20;
println!("Unsigned Integers: x = {}, y = {}", x, y);
Enter fullscreen mode Exit fullscreen mode

➑️ Floating-Point Numbers (f32, f64)

  • f64 is the default type for floats.

Example:

let x: f32 = 10.5;
let y: f32 = 20.5;
println!("Floats: x = {}, y = {}", x, y);
Enter fullscreen mode Exit fullscreen mode

βœ… Edge Cases

  1. Integer overflow example:
let x: u8 = 255;
let y = x + 1; // Causes overflow
println!("{}", y);
Enter fullscreen mode Exit fullscreen mode

➑️ Output: thread 'main' panicked at overflow

  1. Floating-point precision loss:
let x: f32 = 0.1 + 0.2;
println!("{}", x); // Output: 0.30000004
Enter fullscreen mode Exit fullscreen mode

πŸ”£ 5. Booleans in Rust

Rust uses true and false for booleans.

Example:

let is_true: bool = true;
let is_false: bool = false;

if is_true && !is_false {
    println!("Both conditions are satisfied.");
} else {
    println!("Conditions are not satisfied.");
}
Enter fullscreen mode Exit fullscreen mode

βœ… Edge Cases:

  1. Boolean used in if expression:
let condition = 1 > 2;
if condition {
    println!("True");
} else {
    println!("False");
}
Enter fullscreen mode Exit fullscreen mode

🏷️ 6. Variables in Rust

  • Variables are immutable by default.
  • Use mut to make them mutable.

Example:

let x = 10; // immutable
let mut y = 20; // mutable

y = 30; // OK since it's mutable
println!("x = {}, y = {}", x, y);
Enter fullscreen mode Exit fullscreen mode

βœ… Edge Cases:

  1. Trying to modify an immutable variable:
let x = 10;
x = 20; // Compile-time error
Enter fullscreen mode Exit fullscreen mode
  1. Shadowing example:
let x = 10;
let x = x + 20;
println!("{}", x); // Output: 30
Enter fullscreen mode Exit fullscreen mode

πŸ”‘ 7. Strings in Rust

Rust has two types of strings:

  1. String – Heap-allocated and growable
  2. &str – String slice (fixed size)

➑️ Example of String:

let greeting: String = String::from("Hello, Rust!");
println!("{}", greeting);
Enter fullscreen mode Exit fullscreen mode

➑️ Example of &str:

let x: &str = "Hello";
let y: &str = "Rust";
println!("{} {}", x, y);
Enter fullscreen mode Exit fullscreen mode

βœ… Edge Cases:

  1. String indexing is not allowed:
let s = String::from("hello");
println!("{}", s[0]); // Error: Rust strings are UTF-8
Enter fullscreen mode Exit fullscreen mode
  1. String length:
let s = String::from("hello");
println!("{}", s.len()); // Output: 5
Enter fullscreen mode Exit fullscreen mode
  1. Handling Option<char> when indexing:
let s = String::from("rust");
match s.chars().nth(2) {
    Some(c) => println!("Third character: {}", c),
    None => println!("No character at index"),
}
Enter fullscreen mode Exit fullscreen mode

πŸͺ“ 8. Match Statement

Example:

let x = 5;

match x {
    1 => println!("One"),
    2 => println!("Two"),
    3..=5 => println!("Three to Five"),
    _ => println!("Other"),
}
Enter fullscreen mode Exit fullscreen mode

βœ… Edge Cases:

  1. Missing cases:
let x = 7;
match x {
    1 | 2 => println!("One or Two"),
    _ => println!("Other"),
}
Enter fullscreen mode Exit fullscreen mode

πŸ“š 9. Functions in Rust

Example:

fn add(x: i32, y: i32) -> i32 {
    x + y
}

fn main() {
    let result = add(5, 10);
    println!("Sum = {}", result);
}
Enter fullscreen mode Exit fullscreen mode

βœ… Edge Cases:

  1. Mismatched return type:
fn add(x: i32, y: i32) -> i32 {
    x + y;
} // Error due to missing return statement without `;`
Enter fullscreen mode Exit fullscreen mode

πŸ† 10. Full Example

fn main() {
    let x: i32 = 5;
    let y: i32 = 10;
    println!("Sum of {} and {} is {}", x, y, add(x, y));
}

fn add(a: i32, b: i32) -> i32 {
    a + b
}
Enter fullscreen mode Exit fullscreen mode

➑️ Output:

Sum of 5 and 10 is 15
Enter fullscreen mode Exit fullscreen mode

➑️ Clone the repo:

git clone https://github.com/ichiragkumar/rust-basics.git
Enter fullscreen mode Exit fullscreen mode

➑️ Navigate to the project:

cd rust-basics
Enter fullscreen mode Exit fullscreen mode

➑️ Run the project:

cargo run
Enter fullscreen mode Exit fullscreen mode

➑️ Edit and Play Around:

Open src/main.rs in your editor (like VSCode) and start modifying it:

βœ… Add new functions

βœ… Test edge cases

βœ… Break things to see how Rust handles it


πŸš€ Follow and Explore!

πŸ‘‰ Follow Chiragkumar on GitHub and X:

  • GitHub – Star the repo and check out other cool projects! 🌟
  • X (Twitter) – Stay updated with Chiragkumar's coding journey! πŸ§‘β€πŸ’»

Top comments (0)