Programming Language: Rust

Rust is a programming language designed by Mozilla Foundation.

This is the notes on Rust Book.

Cheatsheet

CLI applications:

rustc

rustup

cargo

cargo new <project_name>

cargo build // --release for production

cargo run

cargo check //ensures code compiles but doesn't build the executable.

Hello World

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

Write the code above in hello.rs. Open the terminal, and type: rustc ./hello.rs.

In rust, there must always be a primary function. It's a starting point of the application. The functions must always have curly braces.

Theprintln! is a macro. Macros always end with an exclamation mark.

Hello, Cargo

Cargo is the package and system manager for Rust. It handles downloading libraries, building the binary, and many other tasks.

The libraries that the program uses are called dependencies.

Create a cargo project using cargo new <project_name>.

The cargo automatically creates version control files using Git as a default VCS.

In the project structure using cargo, all files are stored in the srcdirectory. The parameters of the program are defined in the Cargo.toml configuration file.

Build the project using cargo buildcommand. Run the program using cargo run.


Syntax

[associative function]

The string is the standard data type in Rust. :: in String::new() calls the associative function. The associative function is the function that is implemented on the type.

[enum]

Enumerations are called in short as enum, which is the type that can have multiple possible states. We call each possible state a variant.

 

[crate]

The crate is a collection of rust files. 

 

let a: [i32; 5] = [1, 2, 3, 4, 5];

let a: [i32; 5] = [1, 2, 3, 4, 5];
let a: (i32, string, char) = (23, "hello", 'z');

Slices are contiguous sequences of elements in a collection rather than a whole array. It's a kind of a reference.