Learning Rust Programming the old school way!
rustc: The compilercargo: build and the package managercargo new <project name>cargo init-> if project directory already existsCargo.toml-> config fileCargo.lock-> created and maintained bycargocargo build-> creates an executable file intarget/debug/, because the default build is adebugbuild- To run the program
./target/debug/<executable> - Running
cargo build for the first time causesCargoto create a new file at the top level:Cargo.lock.- This file keeps track of the exact versions of dependencies in your project
- Use
cargo runto compile the code and then run the resultant executable all in one command cargo checkcommand checks your code to make sure it compiles but doesn’t produce an executablecargoexpects your source files to live inside thesrcdirectory- The top-level project directory is just for README files, license information, configuration files, and anything else not related to your code.
- Using
cargohelps you organize your projects. There’s a place for everything, and everything is in its place.
- In Rust packages of code are referred to as
crates - Adding package as project dependency:
cargo add <package name> - Installing packages:
cargo install <package name>
macro: macro is like a function, but with an exclamation mark (!) after it. eg:println!()is a macro.- similar to functions (they execute things), but they do not always follow the same rules as functions.
comments: // single line, /* */ multiline
- use the
letkeyword and specify the name of the variable- eg:
let name = "Bob"; - println!("My first name is: {}", name);
- uses
{}as a placeholder in println!() to show variable values. - type of a variable is decided by the value you give it. Rust looks at the value and automatically chooses the right type.
- You can also specify the type eg:
let my_num: i32 = 5;- Basic data types in Rust are divided into different groups:
- Numbers - Whole numbers and decimal numbers (
i32,f64) - Characters - Single letters or symbols (
char) - Strings - Text, a sequence of characters (
&str) - Booleans - True or false values (
bool)
- Numbers - Whole numbers and decimal numbers (
- Basic data types in Rust are divided into different groups:
- eg:
- By default, variables in Rust cannot be changed after they are created:
let x = 5;x = 10; // Error- If you want to change the value of a variable, you must use the mut keyword (which means mutable/changeable):
let mut x = 5;x = 10;
- You must write the type when creating a constant. You cannot let Rust guess the type like you can with regular variables:
const BIRTHYEAR: i32 = 1980;
The most common use of {} is inside macros like println!, format!, or panic!. It acts as a positional placeholder that gets replaced by the value of a variable.
- Basic usage:
println!("Hello, {}!", "world"); - With named arguments:
println!("{name}", name="Alice"); {:?}is the format specifier for the Debug trait, which allows you to print data in a format intended for developers and debugging
examplesdirectory is a standard convention recognized byCargoto store standalone executable files that showcase how to use your library or package- The most common implementation is a standard
.rsfile containing a main function inside theexamples/folder - If an example gets too large, you can create a subfolder inside
examples/.Cargo will automatically seek amain.rsfile inside that subfolder to compile it as the entry point. - To run a specific example:
cargo run --example <example folder name>
- The most common implementation is a standard