codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🦀

Rust

1 / 58 topics
1Getting Started with Rust2Installing Rust3Hello World Program4Rust Syntax5Variables and Mutability6Data Types7Operators
Tutorials/Rust/Getting Started with Rust
🦀Rust

Getting Started with Rust

Updated 2026-04-20
3 min read

Getting Started with Rust

Rust is a systems programming language known for its performance, safety, and concurrency support. It was designed by Mozilla Research and has gained popularity due to its unique approach to memory management and error handling. This tutorial will guide you through the basics of setting up your environment, writing your first program, understanding ownership and borrowing, and exploring Rust's powerful type system.

Prerequisites

Before diving into Rust, ensure you have the following installed:

  • Rust Toolchain: You can install it using rustup, the official toolchain installer.
  • Code Editor/IDE: Visual Studio Code with the Rust Analyzer extension is highly recommended.
  • Basic Understanding of Programming Concepts: Familiarity with concepts like variables, functions, and control flow will be helpful.

Setting Up Your Environment

  1. Install Rust Toolchain: Open your terminal or command prompt and run:

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

    Follow the on-screen instructions to complete the installation.

  2. Verify Installation: Check if Rust is installed correctly by running:

    rustc --version
    cargo --version
    
  3. Set Up Your Code Editor: Install Visual Studio Code and the Rust Analyzer extension for enhanced coding experience.

Writing Your First Program

  1. Create a New Project: Use Cargo, Rust's package manager and build system, to create a new project:

    cargo new hello_world
    cd hello_world
    
  2. Explore the Project Structure: The hello_world directory contains:

    • Cargo.toml: Configuration file for your project.
    • src/main.rs: Entry point of your Rust program.
  3. Write Your First Program: Open src/main.rs and replace its content with:

    fn main() {
        println!("Hello, world!");
    }
    
  4. Build and Run the Program: Use Cargo to build and run your program:

    cargo run
    

    You should see the output:

    Compiling hello_world v0.1.0 (file:///path/to/hello_world)
      Finished dev [unoptimized + debuginfo] target(s) in 1.23s
       Running `target/debug/hello_world`
    Hello, world!
    

Understanding Ownership and Borrowing

Rust's ownership model is a core feature that ensures memory safety without a garbage collector.

Ownership Rules

  • Each value in Rust has an owner.
  • There can only be one owner at a time.
  • When the owner goes out of scope, the value is dropped.

Example: Ownership

fn main() {
    let s1 = String::from("hello");
    let s2 = s1; // Ownership of s1 moves to s2

    println!("{}", s2); // This will work
    // println!("{}", s1); // Error: value borrowed here after move
}

Borrowing

Borrowing allows you to refer to a value without taking ownership.

fn main() {
    let s = String::from("hello");
    let len = calculate_length(&s);

    println!("The length of '{}' is {}.", s, len);
}

fn calculate_length(s: &String) -> usize {
    s.len()
}

Mutable Borrowing

You can also borrow a value mutably.

fn main() {
    let mut s = String::from("hello");
    change(&mut s);

    println!("{}", s); // Outputs "world"
}

fn change(s: &mut String) {
    s.push_str(", world");
}

Exploring Rust's Type System

Rust is statically typed, meaning types are known at compile time.

Basic Types

  • Scalar Types: Integers (i8, u8, i16, etc.), floating-point numbers (f32, f64), booleans (bool), and characters (char).
  • Compound Types: Tuples and arrays.
fn main() {
    let x: (i32, f64, char) = (500, 6.4, 'y');
    let a: [i32; 5] = [1, 2, 3, 4, 5];
}

Enums

Enums are versatile and can be used to represent a value that could be one of several variants.

enum Message {
    Quit,
    Move { x: i32, y: i32 },
    Write(String),
    ChangeColor(i32, i32, i32),
}

Option Type

Option<T> is an enum that encodes the concept of a value being present or absent.

fn main() {
    let some_number = Some(5);
    let some_string = Some("a string");

    let absent_number: Option<i32> = None;
}

Best Practices

  1. Use let for Variable Binding:

    let x = 5; // Immutable binding
    let mut y = 6; // Mutable binding
    
  2. Avoid Dangling References: Ensure that references always point to valid data.

  3. Use Lifetimes for Explicit Borrowing:

    fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
        if x.len() > y.len() {
            x
        } else {
            y
        }
    }
    
  4. Leverage Traits for Code Reusability:

    trait Summary {
        fn summarize(&self) -> String;
    }
    
    struct NewsArticle {
        headline: String,
        location: String,
        author: String,
        content: String,
    }
    
    impl Summary for NewsArticle {
        fn summarize(&self) -> String {
            format!("{}, by {} ({})", self.headline, self.author, self.location)
        }
    }
    

Conclusion

This tutorial has covered the basics of getting started with Rust, including setting up your environment, writing a simple program, understanding ownership and borrowing, and exploring Rust's type system. As you continue learning Rust, focus on mastering its unique features like pattern matching and error handling to become proficient in this powerful language.

For further exploration, consider reading the Rust Book and experimenting with more complex projects.


Next Installing Rust

Recommended Gear

Installing Rust