├── 1-is-even.rs ├── 10-move-value.rs ├── 11-lifetimes.rs ├── 11-mut-reference.rs ├── 12-threads.rs ├── 13-mpsc.rs ├── 2-fib.rs ├── 3-get-str-len.rs ├── 4-struct-user.rs ├── 5-struct-rect-impl.rs ├── 6-calculate-area.rs ├── 7-option.rs ├── 8-result.rs └── 9-date-time.rs /1-is-even.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let ans = is_even(1); 3 | println!("{}", ans); 4 | } 5 | 6 | fn is_even(num: i64) -> bool { 7 | if num % 2 == 0 { 8 | return true; 9 | } else { 10 | return false; 11 | } 12 | } 13 | 14 | -------------------------------------------------------------------------------- /10-move-value.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let mut s1 = String::from("harkirat"); 3 | 4 | s1 = do_something(s1); 5 | println!("number uis {}", s1); 6 | } 7 | 8 | fn do_something(s2: String) -> String { 9 | println!("{}", s2); 10 | return s2; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /11-lifetimes.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let ans; 3 | 4 | let str1 = String::from("small"); 5 | { 6 | let str2 = String::from("longer"); 7 | 8 | ans = longest(&str1, &str2); 9 | } 10 | 11 | println!("{}", ans); 12 | } 13 | 14 | fn longest<'a>(str1: &'a str, str2: &'a str) -> &'a str { 15 | if str1.len() > str2.len() { 16 | return str1; 17 | } else { 18 | return str2; 19 | } 20 | } 21 | 22 | -------------------------------------------------------------------------------- /11-mut-reference.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let mut s1 = String::from("harkirat"); 3 | do_something(&mut s1); 4 | println!("number uis {}", s1); 5 | } 6 | 7 | fn do_something(s2: &mut String) { 8 | s2.push_str(" singh"); 9 | println!("{}", s2); // s2 owns the value 10 | } 11 | 12 | -------------------------------------------------------------------------------- /12-threads.rs: -------------------------------------------------------------------------------- 1 | use std::thread; 2 | 3 | fn main() { 4 | thread::spawn(|| { 5 | let mut c = 0; 6 | for i in 0..500000000 { 7 | for j in 0..500000000 { 8 | c = c + 1; 9 | c = c - 1; 10 | } 11 | } 12 | }); 13 | 14 | thread::spawn(|| { 15 | let mut c = 0; 16 | for i in 0..500000000 { 17 | for j in 0..500000000 { 18 | c = c + 1; 19 | c = c - 1; 20 | } 21 | } 22 | }); 23 | 24 | let mut c = 0; 25 | for i in 0..500000000 { 26 | for j in 0..500000000 { 27 | c = c + 1; 28 | c = c - 1; 29 | // println!("hi from spawned thread {}", i); 30 | } 31 | } 32 | } 33 | 34 | -------------------------------------------------------------------------------- /13-mpsc.rs: -------------------------------------------------------------------------------- 1 | use std::{ 2 | sync::mpsc, 3 | thread::{self, spawn}, 4 | }; 5 | 6 | fn main() { 7 | let (tx, rx) = mpsc::channel(); 8 | 9 | for i in 0..10 { 10 | let producer = tx.clone(); 11 | spawn(move || { 12 | let mut sum: u64 = 0; 13 | for j in i * 10000000..(i + 1 * 10000000) - 1 { 14 | sum = sum + j; 15 | } 16 | producer.send(sum).unwrap(); 17 | }); 18 | } 19 | drop(tx); 20 | 21 | let mut final_sum: u64 = 0; 22 | for val in rx { 23 | println!("recv value from thread"); 24 | final_sum = final_sum + val; 25 | } 26 | println!("{}", final_sum); 27 | } 28 | 29 | -------------------------------------------------------------------------------- /2-fib.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | println!("{}", fib(4)); 3 | } 4 | // 0 1 1 2 3 5 | fn fib(num: u32) -> u32 { 6 | let mut first = 0; 7 | let mut second = 1; 8 | 9 | if num == 0 { 10 | return first; 11 | } 12 | 13 | if num == 1 { 14 | return second; 15 | } 16 | 17 | for _ in 0..(num - 1) { 18 | let temp = second; 19 | second = second + first; 20 | first = temp; 21 | } 22 | return second; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /3-get-str-len.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let name = String::from("harkirat"); 3 | let len = get_str_len(name); 4 | println!("the length of the string is {}", len); 5 | } 6 | 7 | fn get_str_len(str: String) -> usize { 8 | return str.chars().count(); 9 | } 10 | 11 | -------------------------------------------------------------------------------- /4-struct-user.rs: -------------------------------------------------------------------------------- 1 | struct User { 2 | first_name: String, 3 | last_name: String, 4 | age: i32, 5 | } 6 | 7 | fn main() { 8 | let user = User { 9 | first_name: String::from("Harkirat"), 10 | last_name: String::from("Singh"), 11 | age: 32, 12 | }; 13 | 14 | println!("{}", user.first_name); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /5-struct-rect-impl.rs: -------------------------------------------------------------------------------- 1 | struct Rect { 2 | width: i32, 3 | height: i32, 4 | } 5 | 6 | impl Rect { 7 | fn area(&self) -> i32 { 8 | self.width * self.height 9 | } 10 | 11 | fn perimeter(&self, num: i32) -> i32 { 12 | 2 * (self.width + self.height) 13 | } 14 | 15 | fn debug() -> i32 { 16 | return 1; 17 | } 18 | } 19 | 20 | fn main() { 21 | let rect1 = Rect { 22 | width: 10, 23 | height: 20, 24 | }; 25 | 26 | println!("area is {}", rect1.area()); 27 | println!("perimeter is {}", rect1.perimeter(1)); 28 | println!("debug is {}", Rect::debug()); 29 | } 30 | 31 | -------------------------------------------------------------------------------- /6-calculate-area.rs: -------------------------------------------------------------------------------- 1 | enum Shape { 2 | Rectangle(f64, f64), // width, height 3 | Circle(f64), // radius 4 | } 5 | 6 | fn main() { 7 | let rect = Shape::Rectangle(1.0, 2.0); 8 | calculate_area(rect); 9 | let circle = Shape::Circle(1.0); 10 | calculate_area(circle); 11 | } 12 | 13 | fn calculate_area(shape: Shape) -> f64 { 14 | match shape { 15 | Shape::Rectangle(a, b) => a * b, 16 | Shape::Circle(r) => 3.14 * r * r, 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /7-option.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let index = find_first_a(String::from("preet")); 3 | 4 | match index { 5 | Some(value) => println!("index is {}", value), 6 | None => println!("a not found"), 7 | } 8 | } 9 | 10 | fn find_first_a(s: String) -> Option { 11 | for (index, char) in s.chars().enumerate() { 12 | if char == 'a' { 13 | return Some(index as i32); 14 | } 15 | } 16 | 17 | return None; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /8-result.rs: -------------------------------------------------------------------------------- 1 | use std::fs::read_to_string; 2 | 3 | fn main() { 4 | let ans = read_from_file_kirat(String::from("a.txt")); 5 | } 6 | 7 | fn read_from_file_kirat(file_path: String) -> Result { 8 | let result = read_to_string(file_path); // Result 9 | match result { 10 | Ok(data) => Ok(data), 11 | Err(err) => Err(String::from("File not read")), 12 | } 13 | } 14 | 15 | -------------------------------------------------------------------------------- /9-date-time.rs: -------------------------------------------------------------------------------- 1 | use chrono::Utc; 2 | 3 | fn main() { 4 | let now = Utc::now(); 5 | println!("current time is {}", now); 6 | } 7 | 8 | --------------------------------------------------------------------------------