DEV Community

Cover image for Setting Up a New Rust Project with ⚙️ Cargo
Max Zhuk
Max Zhuk

Posted on

Setting Up a New Rust Project with ⚙️ Cargo

To illustrate Rust's capabilities firsthand, let's walk through the process of setting up a new Rust project using Cargo, Rust's package manager and build system. Cargo simplifies many tasks in Rust development, from project creation and building code to adding dependencies and compiling executables.

Step 1: Creating a New Project

First, ensure you have Rust and Cargo installed on your system. You can download them from the official Rust website. Once installed, create a new Rust project by running:

cargo new hello_rust
cd hello_rust
Enter fullscreen mode Exit fullscreen mode

This command creates a new directory called hello_rust, initializes a new Cargo package with a default "Hello, World!" program, and a Cargo.toml file for specifying your project's dependencies.

Step 2: Exploring the Project Structure

The hello_rust project directory contains two main files:

src/main.rs: This is where your Rust source code resides. By default, it contains a simple program that prints "Hello, World!" to the console.

Cargo.toml: This file manages your project's dependencies, metadata, and other configurations.

Step 3: Adding Dependencies

To add a dependency, open Cargo.toml and specify the dependency under [dependencies]. For example, to add the serde library for serialization and deserialization, you would add:

[dependencies]
serde = "1.0"
Enter fullscreen mode Exit fullscreen mode

Step 4: Building and Running Your Project

With the project setup complete, you can build and run your program using Cargo:

cargo run
Enter fullscreen mode Exit fullscreen mode

This command compiles your project and runs the resulting executable, which in this case, prints "Hello, World!" to the console.

Code Example: Actix-web "Hello, World!"

Actix-web is a powerful, pragmatic, and extremely fast web framework for Rust. To get started with an Actix-web "Hello, World!" application, first, we need to add Actix-web as a dependency in our Cargo.toml file:

[dependencies]
actix-web = "4.0"
Enter fullscreen mode Exit fullscreen mode

Next, replace the content of src/main.rs with the following code to create a simple web server that responds with "Hello, World!" to all incoming requests:

use actix_web::{web, App, HttpResponse, HttpServer, Responder};

async fn greet() -> impl Responder {
    HttpResponse::Ok().body("Hello, World!")
}

#[actix_web::main] // Marks the entry point of the application
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        App::new()
            .route("/", web::get().to(greet))
    })
    .bind("127.0.0.1:8080")?
    .run()
    .await
}
Enter fullscreen mode Exit fullscreen mode

This example introduces several key concepts in Actix-web and web development with Rust:

Async/Await: Rust's support for asynchronous programming is front and center, allowing for non-blocking network operations.

Application and Server Configuration: The App struct is used to configure routes and other application settings, while HttpServer handles the actual server setup.

Responders: The Responder trait is a convenient way to generate responses for web requests, showcasing Rust's flexibility and expressiveness.

To run your Actix-web application, use the cargo run command, and visit http://127.0.0.1:8080 in your web browser. You should see the "Hello, World!" message displayed.

Building a decentralized identity management system presents an exciting challenge, one that Rust's features are well-equipped to handle. This section of the article will dive into the foundational building blocks required to construct such a system. We'll explore creating secure authentication methods, managing digital identities, and ensuring user data privacy—all through the lens of Rust's powerful programming capabilities.


Follow me on LinkedIn

Photo by Markus Spiske: https://www.pexels.com/photo/red-and-blue-train-on-railroads-3905831/

Top comments (0)