npm
, called cargo
RLS
), auto formatting (rustfmt
, can also be configured by .rustfmt.toml
) etc//This is a comment
/* This is a block comment
Nice to meet you */
println
macro!
is what denotes the difference b/w a function and a MACRO{:?}
is used to signify to the MACRO we’re going to display something in debug mode//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);
///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
#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
//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;
test
inline module and #[cfg(test)]
to let the compiler know this module is specifically for testing#[test]
macroassert_eq!
macro, it takes in three arguments, what is the result, what result is to be expected and what the error message should be.//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");
}
}
<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 = 5
, it means x
is a immutable variable and is bound to 5 during compile time.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