Introduction

Comments

//This is a comment

/* This is a block comment
	 Nice to meet you */

The println macro

Basic cargo usage

//Starting a new rust project
cargo new <projectName>
cargo new --lib <projectName>

//Running the code
cargo run --bin <fileName>

//Building our project
cargo build
cargo build --release //optimised binary

//check if everything is fine
cargo check

//Take suggestions from the compiler
cargo check --bin <fileName>

//Update the dependencies
cargo update
let life = 42;
println!("hello");
println!("{:?}", life);

Reading documentation

///The following code adds two numbers and returns the result
//The above is not a comment, but rather it will be used to document our code
fn add (a: i32, b: i32) -> i32 {
	a + b
}

cargo doc --open //This would generate the basic documentation including that of any crates that we have included in the TOML file 

Rust development

#clippy
cargo clippy --bin <binary>

#rust-analyzer

#rustfmlt and .rustfmt.toml
cargo fmt

#cargo-outdated: Find outdated dependencies which can't be updated with **cargo update**.
cargo install --locked cargo-outdated
cargo outdated

#cargo-audit: Audit the dependency for vulnerabilities
cargo install -f cargo-audit
cargo audit

Expressions

//In C, an assignment of a variable to its value returns the value of the assignnent
//By which, x = y = 6 becomes legal. In Rust, it's not the case so

let x = (let y = 6); /illegal
let some_variable = 5;

let bool_var = if some_variable < 5 {
	true
} else {
	false // "rohit" here instead of false will lead to compile time error because of the type binding confusion during static type checking
};

let boolB_var = some_variable < 5;

Writing Unit Tests

//Testing a certain program
cargo test --bin <name>
//Testing all the programs
cargo test
fn all_caps(word: &str) -> String {
	word.to_uppercase()
}
#[cfg(test)]
mod test {
	use crate::all_caps;
	#[test] 
	fn check_all_caps() {
		let result = all_caps("hello");
		let expected = "HELLO".to_owned()
		assert_eq!(result, expected, "String should be all uppercase");
	}
}

Variables and data types

Constants and variables

Variables

<aside> 👉 Immutable Variables

</aside>

Their values can’t be changed after they have been assigned a value, and this behaviour is enforced by default.

<aside> 👉 Mutable Variables

</aside>

Their values can be changed after they have been assigned a value, and we can enforce this behaviour by using the mut keyword.

//Implicit Type Declaration
let two = 2;
let hello = "hello";
let j = 'j';
let my_half = 0.5;
let mut my_name = "Bill"
let quit_program = false;

//Explicit Type Declaration
let num: i32 = 10;

//Constant declaration
const NAME_OF_CONSTANT: u32 = 50;

<aside> 👉 Shadowing and compile time binding

</aside>

let x = 10; // x is immutable
x += 5;     // this would lead to a compile time error as we can't change the binding
let x = 15  //this is perfectly legal as the new x variable shadows the previous x variable. This x is different from the previous x.

let spaces = "   ";
let spaces = spaces.len(); //new shadowing the previous spaces

let spaces = "   ";
spaces = spaces.len(); //compile time error because the type binding of spaces is a String, and now we're trying to change

Constants

Primitive data types