DEV Community

Cover image for Genesis in Rust: Getting Started
Dan Mugisho M.
Dan Mugisho M.

Posted on • Edited on

Genesis in Rust: Getting Started

Rust is a systems programming language that focuses on speed, memory safety, and parallelism. Rust is the super productive, super safe, and super fast language that breaks the trend of marginal improvements. It changes the way you think about software development.

Rust puts developers first by providing features that make software development smooth and reliable. This includes maintenance, which is a large part of a project's lifecycle.

This is where Rust shines. Rust combines ease of programming with access to core system configurations. Rust is built with memory-safety, concurrency, and security from the ground up.

Since Rust can run both on the backend (natively) and frontend (via Wasm) of web applications, learning Rust becomes a clear choice for creating performant and reliable web applications that can be accessed anywhere, and on any device.

Rust is used by some of the top tech companies like Amazon, Microsoft Corporation, Dropbox, Cloudflare, Figma, Discord, Mozilla and Facebook to deliver speed and concurrency to their customers.

Let's look at some core features that make Rust stand out from other programming languages.

Performance

Rust is a really fast and memory-efficient language. It offers fine-grained control of memory management and has a minimal standard library. So it can perform well with critical services, within embedded devices, and easily be integrated with other languages.

Security

Using Rust eliminates an entire class of security vulnerabilities from software applications. This helps companies build applications with better performance and higher security. This is a great relief to any new programmer, as it reduces the burden while compiling.

Concurrency

Concurrency is when two or more tasks start, run, and complete in overlapping time. Database operations are a great example to explain concurrency. When thousands of users are using your application at the same time to perform different actions, your database handles them concurrently. Concurrency is a key concept when it comes to scaling applications.

This article will guide you through setting up Rust and getting started.

Getting started

Now that you understand the core features of Rust, let's setting up. You can find installation instructions here follow the instructions to continue the installation.

Now open your IDE, I use WebStorm as my IDE because it provides a robust, fast, and flexible static code analysis and actually provides a lot of support for Rust.

Let's start with a simple β€œHello World!” function. Create a new file with the name "main.rs" extension

Image description

Now open the main.rs and type the following code:

fn main( ) {
    println!("Hello, world!");
}
Enter fullscreen mode Exit fullscreen mode
  • fn is used to declare a function in Rust
  • The main function is special: it's the beginning of every Rust program
  • The line ends with a semicolon (;). The ; indicates that this expression is over, and the next one is ready to begin. Most lines of Rust code end with a ;

Save the file, open your terminal and enter the following commands:

$   rustc main.rs
$   ./main
Enter fullscreen mode Exit fullscreen mode
  • Before running a Rust program, you have to compile it. You can use the Rust compiler by entering the rustc command and passing it the name of your source file

And you should see the string Hello, world! print to the terminal. If you did, then congratulations! You've officially written a your first Rust program πŸ‘πŸŽŠ

Project management with Cargo

To create your first project using Cargo, you invoke the cargo executable with keyword new and lastly with the name of the project like so:

$   cargo new project-name
Enter fullscreen mode Exit fullscreen mode

Let’s check out what Cargo has generated for us:

$   cd project-name
$   tree .
 .
 β”œβ”€β”€ Cargo.toml
 └── src
       └── main.rs

1 directory, 2 files
Enter fullscreen mode Exit fullscreen mode

Image description

The main.rs is our projects main file, the entry for our app. Here's what main.rs contains:

fn main( ) {
    println!("Hello, world!");
}
Enter fullscreen mode Exit fullscreen mode
  • Cargo generated a hello, world! for us

Let’s take a closer look at Cargo.toml :


   [package]
   name = "project-name"
   version = "0.1.0"
   authors = ["email, inferred from Git"]
   edition = "2018"

   # See more keys and their definitions at https://doc.rustlang.org/cargo/reference/manifest.html

   [dependencies]
Enter fullscreen mode Exit fullscreen mode
  • This is called a manifest, and it contains all of the metadata that Cargo needs to compile your project.

Now let’s build and run the code

To build and run your project, call cargo run in your project directory root:

$   cargo run
Enter fullscreen mode Exit fullscreen mode

You should get this output :

Image description

Resources to help you

Afterward, practice coding with Exercism to improve your Rust chops together with reading a more extensive resource like the Rust book (standard choice) or Easy Rust (accessible choice). At any point where you feel that you are almost ready to build your own toy applications, just go ahead and do it. There are some nice online communities to ask for feedback and support, so don’t be worried about getting stuck!


☞ Follow me on Twitter & Linkedin

☞ Kindly subscribe for my upcoming articles

Top comments (0)