Getting started with Rust

Getting Started with Rust

Rust is a systems programming language focused on safety, speed, and concurrency. This guide will help you set up your Rust environment and write your first Rust program.

1. Install Rust

Using rustup

The recommended way to install Rust is through rustup, a toolchain installer for Rust. It manages Rust versions and associated tools.

  1. Open your terminal.
  2. Run the following command:

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

  3. Follow the on-screen instructions. This will install the latest stable version of Rust.

  4. After installation, ensure that your PATH is set up correctly. You may need to restart your terminal or run:

    bash source $HOME/.cargo/env

Verify Installation

To verify that Rust is installed correctly, run:

bash rustc --version

You should see the version of Rust that you installed.

2. Set Up Your Development Environment

Text Editor/IDE

You can use any text editor or IDE of your choice. Some popular options for Rust development include:

  • Visual Studio Code: With the Rust extension for syntax highlighting and code completion.
  • IntelliJ IDEA: With the Rust plugin.
  • Sublime Text: With Rust packages.

Install Rust Language Server (RLS)

For better development experience, you can install the Rust Language Server:

bash rustup component add rls rust-analysis rust-src

3. Create Your First Rust Project

Using Cargo

Cargo is Rust's package manager and build system. It simplifies the process of managing Rust projects.

  1. Create a new project:

    bash cargo new hello_rust

    This creates a new directory called hello_rust with a basic project structure.

  2. Navigate to your project directory:

    bash cd hello_rust

Project Structure

The generated project will have the following structure:

hello_rust/ ├── Cargo.toml └── src └── main.rs

  • Cargo.toml: The configuration file for your project, where you can specify dependencies and project metadata.
  • src/main.rs: The main source file for your Rust application.

4. Write Your First Program

Open src/main.rs in your text editor and replace its contents with the following code:

rust fn main() { println!("Hello, world!"); }

5. Build and Run Your Program

To build and run your program, use the following command:

bash cargo run

You should see the output:

Hello, world!

6. Learn Rust

Now that you have a basic setup, you can start learning Rust. Here are some resources:

  • The Rust Programming Language Book: Often referred to as "The Book," it is the official guide to Rust. Read it online.
  • Rust by Example: A collection of runnable examples that illustrate various Rust concepts. Check it out.
  • Rustlings: Small exercises to get you used to reading and writing Rust code. Find it here.

7. Join the Community

Rust has a friendly and welcoming community. You can join discussions and ask questions in various places:

Conclusion

Congratulations! You have successfully set up Rust and created your first program. Continue exploring the language and its features, and happy coding!

Comments (3)

  • Konrad Reply Det ser godt ud! mandag den 1. januar 0001 00.00
  • Anne Dam Reply Ja, det kan noget det Rust!? mandag den 1. januar 0001 00.00
  • Steen Secher Reply Rust er helt klart en lovende teknologi. Som jeg plejer at sige det: Det er helt Secher't! onsdag den 19. marts 2025 10.33

Leave a comment