close icon
daily.dev platform

Discover more from daily.dev

Personalized news feed, dev communities and search, much better than whatโ€™s out there. Maybe ;)

Start reading - Free forever
Start reading - Free forever
Continue reading >

Set Up Rust Development Environment: Quick Guide

Set Up Rust Development Environment: Quick Guide
Author
Nimrod Kramer
Related tags on daily.dev
toc
Table of contents
arrow-down

๐ŸŽฏ

Quickly set up your Rust development environment with essential tools and learn to write your first program for powerful and safe coding.

Here's how to get started with Rust:

  1. Install Rust via rustup (https://rustup.rs/)
  2. Set up VS Code with Rust extensions
  3. Learn Cargo basics
  4. Write your first Rust program
  5. Use rustfmt and Clippy

Key tools:

  • rustc (compiler)
  • Cargo (package manager)
  • VS Code (recommended editor)
  • rustfmt (formatter)
  • Clippy (linter)

Quick start:

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

# New project
cargo new hello_world
cd hello_world

# Build and run
cargo run

Let's walk through setting up Rust, using key tools, and writing your first program.

What You Need Before Starting

Make sure you have:

Computer Requirements

Component Minimum Recommended
OS Windows 8.1 64-bit / macOS 10.11 Latest Windows/macOS
CPU Intel i7-3770 / AMD FX-9590 Intel i7-4790K / AMD Ryzen 5 1600
RAM 10 GB 16 GB
Storage 25 GB free 25 GB SSD

SSD recommended for faster loads.

Basic Command-Line Skills

You should know how to:

  • Navigate directories
  • Create/delete files and folders
  • Run commands
  • Understand file permissions

To install Rust:

$ curl https://sh.rustup.rs -sSf | sh

This installs rustc and cargo.

On macOS, you may need Xcode tools:

$ xcode-select --install

How to Install Rust

Rust

Here's how to get Rust running:

Using rustup to Install

rustup

  1. Open your terminal
  2. Run:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
  1. Follow the prompts

Windows Installation

  1. Download rustup-init.exe
  2. Run it and choose the default option

macOS/Linux Installation

Use the curl command above.

Checking Your Installation

Open a new terminal and run:

rustc --version

If you see a version number, you're set.

Setting Up Your Code Editor

VS Code is great for Rust. To set it up:

  1. Install VS Code
  2. Install the rust-analyzer extension
  3. Install CodeLLDB for debugging

Useful add-ons:

  • Even Better TOML
  • crates
  • Error Lens

Test your setup with a simple "Hello World" program.

Using Cargo: Rust's Package Manager

Cargo

Cargo handles building, dependencies, and more.

To start a new project:

$ cargo new hello_rust

This creates:

hello_rust/
โ”œโ”€โ”€ Cargo.toml
โ””โ”€โ”€ src/
    โ””โ”€โ”€ main.rs

The Cargo.toml file manages your project:

[package]
name = "hello_rust"
version = "0.1.0"
edition = "2021"

[dependencies]

Add dependencies under [dependencies].

Key Cargo commands:

Command Description
cargo build Compiles project
cargo run Compiles and runs
cargo test Runs tests
cargo check Checks for errors

Writing Your First Rust Program

Let's make a "Hello, World!" app:

  1. Create main.rs:
fn main() {
    println!("Hello, world!");
}
  1. Compile and run:
$ rustc main.rs
$ ./main

Or with Cargo:

$ cargo run
sbb-itb-bfaad5b

Finding and Fixing Bugs in Rust

Use these methods to debug:

  1. println! macro for tracking values
  2. Implement Debug trait for easy printing
  3. Write tests with #[test] attribute
  4. Use Result type for error handling
  5. Add logging with the log crate

Working with Dependencies

Add libraries to Cargo.toml:

[dependencies]
time = "0.1.12"

Or use cargo add:

cargo add regex

Update dependencies:

cargo update

Keeping Your Code Clean

Use rustfmt to format code:

cargo fmt

Use Clippy to check for issues:

cargo clippy

Testing Your Rust Code

Write tests in your code:

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
}

Run tests with:

cargo test

Tips for Good Rust Programming

  • Use modules to organize code
  • Handle errors with Result type
  • Write clear documentation

Solving Common Problems

For help, check:

  • Official Rust Forum
  • Rust Discord
  • Stack Overflow
  • Reddit (r/rust, r/learnrust)

Keep coding and enjoy Rust's power and safety!

Related posts

Why not level up your reading with

Stay up-to-date with the latest developer news every time you open a new tab.

Read more