Rust is static types language
Rustc compiler convert rust to executable
cargo rust build system and pkg manager
cargo new projectname (like npm in js)
cargo.tomo → (similar to pkg.json file in js)
cargo run filename → compile to bin and run
rustc filename →convert to bin and we need to run
Varaibles
are imutable by default
const my need to define the vairable types
Types
Data type of every expression must be known at compile time.we don’t need to explicitly specify the type of every variable, but the type system will infer the types based on how the variables are used. This is known as type inference.
Scalar Types
Integers:
i32: 32-bit signed integer (default)
i64: 64-bit signed integer
u32: 32-bit unsigned integer
u64: 64-bit unsigned integer
isize: Pointer-sized signed integer
usize: Pointer-sized unsigned integer
Floats:
f32: 32-bit floating-point number
f64: 64-bit floating-point number (default)
Characters:
char: Unicode scalar value (e.g., ‘a’, ’😊’)
Example: let x: char = 'a';
Boolean:
bool: true or false
Example: let x: bool = true;
Compound Types
Structs:
A custom data type with named fields
Example
String : have two imutable fixed length and growable heap allocated data structure
Loops
Macro
In Rust, a macro is a way to extend the language itself. Macros are essentially functions that generate code at compile-time, allowing you to create new syntax, abstractions, and domain-specific languages (DSLs) within Rust.
Macros are defined using the macro keyword, followed by the name of the macro and a set of rules that define how the macro should be expanded. When the macro is invoked, the Rust compiler will replace the macro invocation with the expanded code.
Macros need to be call with !
Types of Macros in Rust:
Declarative Macros: These macros are defined using the macro_rules! syntax and are used to generate code at compile-time.
Procedural Macros: These macros are defined using the proc_macro attribute and are used to generate code at compile-time using a procedural interface.
Attribute Macros: These macros are used to attach attributes to items, such as functions or structs.
Builtin Macro
1. println!: Prints its arguments to the standard output, followed by a newline character.
2. vec!: Creates a new vector with the specified elements.
3. format!: Formats its arguments into a string.
4. assert!: Asserts that a condition is true, and panics if it’s false.
5. assert_eq!: Asserts that two values are equal, and panics if they’re not.
6. assert_ne!: Asserts that two values are not equal, and panics if they are.
7. panic!: Panics with a custom message.
8. unimplemented!: Panics with a message indicating that a function or method is not implemented.
9. todo!: Panics with a message indicating that a function or method is not implemented, and suggests that it should be implemented.
10. include!: Includes the contents of a file into the current module.
11. include_str!: Includes the contents of a file as a string.
12. module!: Defines a new module.
13. concat!: Concatenates its arguments into a single string.
14. stringify!: Converts its argument into a string.
15. debug_assert!: Asserts that a condition is true, and panics if it’s false, but only in debug builds.