My Rust Learnings : Introduction

Introduction

Rust was initially developed to build high-programming applications without the issue of invalid memory access that developers were facing while using C and C++, However It also ensure high performance similar to that offered by C and C++ It provide high performance while processing large amounts of data, support for concurrent programming, and this together with an effective compiler are other reasons why well-known software heavyweights now use this programming language.

Elements of Rust

  • cargo - Build system and package manager for rust.
  • rustfmt - Coding style formatting tool.
  • rustc - Compiler.
  • rustup - Tool for managing rust versions and associated tools.

Hello, World!

Writing simple program with source file name main.rs. Here .rs is extension of rust file.

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

To compile and run the rust program.

1
2
3
$ rustc main.rs
$ ./main
Hello, world!

Cargo!

Cargo is Rust’s build system and package manager. Cargo handles a lot of tasks, such as building code, downloading the libraries code depends on, and building those libraries.

Creating new project “hello_world” with cargo.

1
$ cargo new hello_world

This is the structure of hello_world directory.

.
├── Cargo.toml
└── src

   └── main.rs

Tada, this is the code of Cargo.toml file.

1
2
3
4
5
6
7
8
[package]
name = "hello_world"
version = "0.1.0"
edition = "2021"

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

[dependencies]
  • [package] is a section heading that indicates that the following statements are configuring a package.
  • [dependencies] is the start of a section for the list any of your project’s dependencies.
  • cargo build - It will compile and creates an executable file in target/debug/.
  • cargo run - It will compile and run the executable in one command.
  • cargo check - This command quickly checks the code to make sure it compiles but doesn’t produce an executable