├── .gitignore ├── README.md ├── Cargo.toml ├── Cargo.lock └── src ├── main.rs ├── vars.rs ├── print.rs ├── types.rs └── strings.rs /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RustCrashCourse 2 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rust" 3 | version = "0.1.0" 4 | authors = ["Olimjon Sadykov "] 5 | edition = "2021" 6 | 7 | [dependencies] 8 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "rust" 7 | version = "0.1.0" 8 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | // mod print; 2 | // mod vars; 3 | // mod types; 4 | mod strings; 5 | 6 | fn main() { 7 | // print::run() 8 | // vars::run() 9 | // types::run() 10 | strings::run() 11 | } 12 | -------------------------------------------------------------------------------- /src/vars.rs: -------------------------------------------------------------------------------- 1 | // Variables hold primitive data or reference to data 2 | // Variables are immutable by default 3 | // Rust is a block-scoped language 4 | 5 | pub fn run() { 6 | let name = "Brad"; 7 | let mut age = 37; 8 | println!("My name is {} and I am {}", name, age); 9 | age = 38; 10 | println!("My name is {} and I am {}", name, age); 11 | 12 | // Define constant 13 | const ID: i32 = 001; 14 | println!("ID: {}", ID); 15 | 16 | // Assign multiple vars 17 | let (my_name, my_age) = ("Brad", 37); 18 | println!("{} is {}", my_name, my_age) 19 | } 20 | -------------------------------------------------------------------------------- /src/print.rs: -------------------------------------------------------------------------------- 1 | pub fn run() { 2 | // Print to console 3 | println!("Hello from print.rs file"); 4 | 5 | // Basic Formatting 6 | println!("{} is from {}", "Brad", "Mass"); 7 | 8 | // Positional Arguments 9 | println!( 10 | "{0} is from {1} and {0} likes to {2}", 11 | "Brad", "Mass", "code" 12 | ); 13 | 14 | // Named Arguments 15 | println!( 16 | "{name} likes to play {activity}", 17 | name = "John", 18 | activity = "Baseball" 19 | ); 20 | 21 | //Placeholder traits 22 | print!("Binary: {:b} Hex: {:x} Octal {:o}", 10, 10, 10); 23 | 24 | // Placeholderfot debug trait 25 | println!("{:?}", (12, true, "hello")); 26 | 27 | // Basic math 28 | print!("10 + 10 = {}", 10 + 10) 29 | } 30 | -------------------------------------------------------------------------------- /src/types.rs: -------------------------------------------------------------------------------- 1 | /* 2 | Primitive Types-- 3 | Integers: u8, i8, u16, i16, u32, i32, u64, i64, u128, i128 (number of bits they take in 4 | memory) 5 | Floats: f32, f64 6 | Boolean: (bool) 7 | Characters: (char) 8 | Tuples 9 | Arrays 10 | */ 11 | 12 | // Rust is atatically type languag, which means that it must know the type of all 13 | // variables at compile time, however, the compileran usuallyinfer what type we want to use 14 | // based on the value and how use it. 15 | 16 | pub fn run() { 17 | // Default is "i32" 18 | let x = 1; 19 | 20 | // Default is "f64" 21 | let y = 2.5; 22 | 23 | // Add explicit type 24 | let z: i64 = 454545454545; 25 | 26 | // Find max size 27 | println!("Max i32: {}", std::i32::MAX); 28 | println!("Max i64: {}", std::i64::MAX); 29 | 30 | // Boolean 31 | let is_active: bool = true; 32 | 33 | // Get boolean from expression 34 | let is_greater = 10 < 5; 35 | 36 | let a1 = 'a'; 37 | let face = '\u{1F600}'; 38 | 39 | println!("{:?}", (x, y, z, is_active, is_greater, a1, face)) 40 | } 41 | -------------------------------------------------------------------------------- /src/strings.rs: -------------------------------------------------------------------------------- 1 | // Primitive str = Immutable fixed-length string somewhere in memory 2 | // String = Growable, heap-allocated data structure - Use when you need to modify or own 3 | // string date 4 | 5 | pub fn run() { 6 | let mut hello = String::from("Hello "); 7 | 8 | // Get length 9 | println!("Length: {}", hello.len()); 10 | 11 | // Push char 12 | hello.push('W'); 13 | 14 | // Push string 15 | hello.push_str("orld!"); 16 | 17 | // Capacity in bytes 18 | println!("Capacity: {}", hello.capacity()); 19 | 20 | // Check if empty 21 | println!("Is Empty: {}", hello.is_empty()); 22 | 23 | // Contains 24 | println!("Contains 'World' {}", hello.contains("World")); 25 | 26 | // Replace 27 | println!("Replace: {}", hello.replace("World", "There")); 28 | 29 | // Loop through string by whitespace 30 | for word in hello.split_whitespace() { 31 | println!("{}", word); 32 | } 33 | 34 | // Create string with capacity 35 | let mut s = String::with_capacity(10); 36 | s.push('a'); 37 | s.push('b'); 38 | 39 | // Assertion testing 40 | assert_eq!(2, s.len()); 41 | assert_eq!(11, s.capacity()); 42 | 43 | println!("{}", s); 44 | } 45 | --------------------------------------------------------------------------------