DEV Community

Cover image for Rust Part 1
Muhammed Sabith
Muhammed Sabith

Posted on

Rust Part 1

Quick Info

A part by part series of learning in Rust by @masterdevsabith

Announcement

Recently I've started learning Rust. So I thought it would be nice, if I could share my learnings here as well. Also to point out the mistakes and problems I've made and faced.

Also before continuing, I'm telling you that I will not be consistent in making this part by part articles. I'll write this, whenever I'm getting time.

Introduction

Rust is a modern, statically-typed systems programming language known for its focus on safety, performance, and concurrency. It was originally developed at Mozilla and is designed to be a safer alternative to languages like C and C++. Also it's a compiled programming language.

Key characteristics

  • Memory safety
  • High performance
  • Systems-level control
  • Great tooling (Cargo package manager)

  • etc..

Installation

For installing you can use this guide

during some part of installation, it prompts you to install visual studio, but that's not needed, as you only need to install the visual studio build tools. You can download it directly from visual studio build tools website

during installation, check these things

  • Desktop development with C++
  • Windows 10/11 SDK
  • MSVC v143 build tools

Hello World !

Now let's start a project and run hello world from it.

  1. create a folder named projects for putting all your rust projects
  2. create a folder named hello_world (this is your first project)
  3. Now, make a new source file and call it main.rs. (rust files end with .rs extension)
  4. Now, paste in this code in that main.rs file :

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

Enter fullscreen mode Exit fullscreen mode
  1. Save the file, go to terminal, in ~/projects/hello_world directory, and enter the following commands to compile and run the file :
$ rustc main.rs
$ ./main
Hello, world!
Enter fullscreen mode Exit fullscreen mode

On Windows, enter the command .\main instead of ./main:

  1. Running this should output "Hello World". Finally you can call yourself a Rust programmer.

Analysing Code

we're going to review the above program for understanding more details.

fn main() {

}
Enter fullscreen mode Exit fullscreen mode

these lines define a function named main and it's the entrypoint (It runs first in every executable Rust program).

  • the first line declares a function named main using the fn keyword, and that function has no parameters, if it had, that would go inside the parentheses ().

  • The function body is wrapped in {}. Rust requires curly brackets around all function bodies.


The body of the main function holds the following code:

println!("Hello, world!");
Enter fullscreen mode Exit fullscreen mode

This line does all the work in this little program: it prints text to the screen.

  • println! calls a Rust Macro, for now just understand that using a ! means that you’re calling a macro instead of a normal function and that macros don’t always follow the same rules as functions.

  • you see the "Hello, world!" string. We pass this string as an argument to println!, and the string is printed to the screen.

  • we end the line with a semicolon (;), which indicates that this expression is over and the next one is ready to begin. Most lines of Rust code end with a semicolon. (There are places where you should not use semicolon).

Compiling and Running Are Separate Steps

Before running a Rust program, you must compile it using the Rust compiler by entering the rustc command and passing it the name of your source file, like this:

$ rustc main.rs
Enter fullscreen mode Exit fullscreen mode

for simple programs, compiling with rustc is enough
but for larger projects, you'll have to do something, in next part we'll learn cargo tool just like npm for js.

Socials

drop me a follow on both of this 👇

Github : @masterdevsabith
Twitter/X : @masterdevsabith

Top comments (0)