diff --git a/02-guessing_game/src/main.rs b/02-guessing_game/src/main.rs index e779b46..c0bd289 100644 --- a/02-guessing_game/src/main.rs +++ b/02-guessing_game/src/main.rs @@ -1,38 +1,31 @@ //Libs -use std::io; -use std::cmp::Ordering; use rand::Rng; -//Funcs -fn main() - { - println!("Hello, world!\n"); - let secret_number = rand::thread_rng().gen_range(1..=10); - println!("\t--> Guessing Game <--"); - println!("Secret Number = {secret_number}"); - loop - { - println!("Give a guess ↓ "); - let mut guess = String::new(); - io::stdin() - .read_line(&mut guess) - .expect("Failed to read line"); - let guess: u32 = match - guess.trim() - .parse() - { - Ok(num) => num, - Err(_) => continue, - }; - println!("You Guessed = {guess}"); - match guess.cmp(&secret_number) - { - Ordering::Less => println!("Too small"), - Ordering::Greater => println!("Too big"), - Ordering::Equal => - { - println!("You win"); - break; - } - } +use std::cmp::Ordering; +use std::io; +//Functions +fn main() { + println!("Hello, world!\n"); + let secret_number = rand::thread_rng().gen_range(1..=10); + println!("\t--> Guessing Game <--"); + println!("Secret Number = {secret_number}"); + loop { + println!("Give a guess ↓ "); + let mut guess = String::new(); + io::stdin() + .read_line(&mut guess) + .expect("Failed to read line"); + let guess: u32 = match guess.trim().parse() { + Ok(num) => num, + Err(_) => continue, + }; + println!("You Guessed = {guess}"); + match guess.cmp(&secret_number) { + Ordering::Less => println!("Too small"), + Ordering::Greater => println!("Too big"), + Ordering::Equal => { + println!("You win"); + break; } + } } +} diff --git a/04-data_types/src/main.rs b/04-data_types/src/main.rs index 146f41f..de7e3a6 100644 --- a/04-data_types/src/main.rs +++ b/04-data_types/src/main.rs @@ -2,59 +2,65 @@ fn main() { println!("Hello, world!"); //Integer - let a:i8 = 127; //(2^7-1) - let au:u8 = 255; //(2^8-1) - println!("a={a}\nau={au}"); + let a: i8 = 127; //(2^7-1) + let au: u8 = 255; //(2^8-1) + println!("a={a}\nau={au}"); - let b:i16 = 32767; //(2^15-1) - let bu:u16 = 65535; //(2^16-1) - println!("b={b}\nbu={bu}"); + let b: i16 = 32767; //(2^15-1) + let bu: u16 = 65535; //(2^16-1) + println!("b={b}\nbu={bu}"); - let c:i32 = 2147483647; //(2^31-1) - let cu:u32 = 4294967295; //(2^32-1) - println!("c={c}\ncu={cu}"); + let c: i32 = 2147483647; //(2^31-1) + let cu: u32 = 4294967295; //(2^32-1) + println!("c={c}\ncu={cu}"); - let d:i64 = 9223372036854775807; //(2^63-1) - let du:u64 = 18446744073709551615; //(2^64-1) - println!("d={d}\ndu={du}"); + let d: i64 = 9223372036854775807; //(2^63-1) + let du: u64 = 18446744073709551615; //(2^64-1) + println!("d={d}\ndu={du}"); - let e:i128 = 170141183460469231731687303715884105727; //(2^127-1) - let eu:u128 = 340282366920938463463374607431768211455; //(2^128-1) - println!("e={e}\neu={eu}"); + let e: i128 = 170141183460469231731687303715884105727; //(2^127-1) + let eu: u128 = 340282366920938463463374607431768211455; //(2^128-1) + println!("e={e}\neu={eu}"); - let f:isize = d as isize; //Based on Architechture of CPU = i64 for me - let fu:usize = du as usize; //Also Explicit Conversion - println!("f={f}\nfu={fu}"); + let f: isize = d as isize; //Based on Architecture of CPU = i64 for me + let fu: usize = du as usize; //Also Explicit Conversion + println!("f={f}\nfu={fu}"); //Float - let g:f32 = 0.123456789123456789; //7-precision I think - let h:f64 = 0.123456789123456789; //17-precision I think - println!("g={g}\nh={h}"); + let g: f32 = 0.123456789123456789; //7-precision I think + let h: f64 = 0.123456789123456789; //17-precision I think + println!("g={g}\nh={h}"); //Boolean - let i = true; - let j = false; - println!("i={i}\nj={j}"); + let i = true; + let j = false; + println!("i={i}\nj={j}"); //Character - let k = 'K'; - let l = '🦀'; - println!("k={k}\nl={l}"); + let k = 'K'; + let l = '🦀'; + println!("k={k}\nl={l}"); //Tuple - let tup = ("Tahinli",13,3.14); - println!("Tuple = {}-{}-{}", tup.0, tup.1, tup.2); - let (x,y,z) = tup; //Destructuring - println!("X-Y-Z = {x}-{y}-{z}"); + let tup = ("Tahinli", 13, 3.14); + println!("Tuple = {}-{}-{}", tup.0, tup.1, tup.2); + let (x, y, z) = tup; //Destructuring + println!("X-Y-Z = {x}-{y}-{z}"); //Array - let ar = [0,1,2,3,4]; - //I havent learn finite loop in Rust sorry. - println!("ar[0]={}\nar[1]={}\nar[2]={}\nar[3]={}\nar[4]={}",ar[0], ar[1], ar[2], ar[3], ar[4]); + let array_1 = [0, 1, 2, 3, 4]; + //I haven't learn finite loop in Rust sorry. + println!( + "array_1[0]={}\narray_1[1]={}\narray_1[2]={}\narray_1[3]={}\narray_1[4]={}", + array_1[0], array_1[1], array_1[2], array_1[3], array_1[4] + ); - let arr = [13;5]; - println!("arr[0]={}\narr[1]={}\narr[2]={}\narr[3]={}\narr[4]={}",arr[0], arr[1], arr[2], arr[3], arr[4]); + let array_2 = [13; 5]; + println!( + "array_2[0]={}\narray_2[1]={}\narray_2[2]={}\narray_2[3]={}\narray_2[4]={}", + array_2[0], array_2[1], array_2[2], array_2[3], array_2[4] + ); - let arrr : [f64;2] = [0.1,2.3]; - println!("arrr[0]={}\narrr[1]={}", arrr[0], arrr[1]); + let array_3: [f64; 2] = [0.1, 2.3]; + println!("array_3[0]={}\narray_3[1]={}", array_3[0], array_3[1]); } diff --git a/05-functions/src/main.rs b/05-functions/src/main.rs index 76b26c9..f92beed 100644 --- a/05-functions/src/main.rs +++ b/05-functions/src/main.rs @@ -1,26 +1,22 @@ -fn main() - { - hello(); +fn main() { + hello(); - let x = { - let y = 5; //Statement - y+0 //Expression - }; - println!("X = {}", x); + let x = { + let y = 5; //Statement + y + 0 //Expression + }; + println!("X = {}", x); + let y = return_value(x); + println!("Y = {}", y) +} - let y = return_value(x); - println!("Y = {}", y) - } +fn hello() { + println!("Hello World"); +} -fn hello() - { - println!("Hello World"); - } - -fn return_value(mut x:i32) -> i32 - { - println!("We're going to return something"); - x = x+1; - x*x - } \ No newline at end of file +fn return_value(mut x: i32) -> i32 { + println!("We're going to return something"); + x = x + 1; + x * x +} diff --git a/06-control_flow/src/main.rs b/06-control_flow/src/main.rs index 82400de..870577f 100644 --- a/06-control_flow/src/main.rs +++ b/06-control_flow/src/main.rs @@ -3,55 +3,46 @@ fn main() { let x = 3; - if x<0 - { - println!("Below Zero"); - } - else if x==0 - { - println!("Equals Zero"); - } - else - { - println!("Above Zero"); - } + if x < 0 { + println!("Below Zero"); + } else if x == 0 { + println!("Equals Zero"); + } else { + println!("Above Zero"); + } let val = true; - let x = if val {5} else {7}; + let x = if val { 5 } else { 7 }; - println!("X is = {x}"); - - 'outer_loop: loop //Labeling a loop - { - println!("Outer Loop"); - 'inner_loop: loop - { - println!("Inner Loop"); - if x==5 - { - break 'outer_loop; - } - else - { - break 'inner_loop; - } - } + println!("X is = {x}"); + + 'outer_loop: loop + //Labeling a loop + { + println!("Outer Loop"); + 'inner_loop: loop { + println!("Inner Loop"); + if x == 5 { + break 'outer_loop; + } else { + break 'inner_loop; + } } + } let mut y = 0; - while y==0 - { - y += 1; - } - let q = [0,1,2,3,4]; - - for element in q //For Each - { - println!("Element of Array = {element}"); - } - - for element in (0..100).rev() //Standart For - { - println!("Element = {element}"); - } + while y == 0 { + y += 1; + } + let q = [0, 1, 2, 3, 4]; + for element in q + //For Each + { + println!("Element of Array = {element}"); + } + for element in (0..100).rev() + //Standard For + { + println!("Element = {element}"); + } } diff --git a/07-temperature_convertion/src/main.rs b/07-temperature_convertion/src/main.rs index ea8942b..337b7c6 100644 --- a/07-temperature_convertion/src/main.rs +++ b/07-temperature_convertion/src/main.rs @@ -1,52 +1,45 @@ use std::io; -fn get_input() -> i32 - { - loop - { - let mut input = String::new(); - io::stdin() - .read_line(&mut input) - .expect("Failed to read line"); - match input.trim().parse() - { - Ok(num) => return num, - Err(_) => - { - println!("Expected Valid Number"); - } - }; +fn get_input() -> i32 { + loop { + let mut input = String::new(); + io::stdin() + .read_line(&mut input) + .expect("Failed to read line"); + match input.trim().parse() { + Ok(num) => return num, + Err(_) => { + println!("Expected Valid Number"); } + }; } -fn main() - { - println!("Hello, world!"); +} +fn main() { + println!("Hello, world!"); - println!("1) Fahrenheit to Celsius\n2) Celsius to Fahrenheit"); - let mut selection; - let mut b = true; - while b - { + println!("1) Fahrenheit to Celsius\n2) Celsius to Fahrenheit"); + let mut selection; + let mut b = true; + while b { + selection = get_input(); + match selection { + 1 => { + println!("Fahrenheit Value = ↓"); selection = get_input(); - match selection - { - 1 => - { - println!("Fahrenheit Value = ↓"); - selection = get_input(); - println!("Celsius Value = {}", (selection-32) as f32*5.0/9.0); - b = false; - }, - 2 => - { - println!("Celsius Value = ↓"); - selection = get_input(); - println!("Fahrenheit Value = {}", (selection as f32*9.0/5.0)+32.0); - b = false; - }, - _ => - { - println!("Expected Valid Value"); - }, - } + println!("Celsius Value = {}", (selection - 32) as f32 * 5.0 / 9.0); + b = false; } + 2 => { + println!("Celsius Value = ↓"); + selection = get_input(); + println!( + "Fahrenheit Value = {}", + (selection as f32 * 9.0 / 5.0) + 32.0 + ); + b = false; + } + _ => { + println!("Expected Valid Value"); + } + } } +} diff --git a/08-fibonacci/src/main.rs b/08-fibonacci/src/main.rs index 9dd08a4..9a73d37 100644 --- a/08-fibonacci/src/main.rs +++ b/08-fibonacci/src/main.rs @@ -1,38 +1,31 @@ use std::io; -fn get_input()->u32 - { - loop - { - let mut input = String::new(); - io::stdin() - .read_line(&mut input) - .expect("Failed to read line"); - match input.trim().parse() - { - Ok(num) => return num, - Err(_) => - { - println!("Expected Valid Number"); - } - }; - } - } -fn fibo(val:u32)->u128 - { - let mut a = 0; - let mut b = 1; - let mut c; - for _element in 2..=val - { - c = a+b; - a = b; - b = c; +fn get_input() -> u32 { + loop { + let mut input = String::new(); + io::stdin() + .read_line(&mut input) + .expect("Failed to read line"); + match input.trim().parse() { + Ok(num) => return num, + Err(_) => { + println!("Expected Valid Number"); } - return b; + }; } -fn main() - { - println!("Hello, world!"); - println!("Give a Number for Fibonacci Calculation ↓"); - println!("Result = {}",fibo(get_input())); +} +fn fibonacci(val: u32) -> u128 { + let mut a = 0; + let mut b = 1; + let mut c; + for _element in 2..=val { + c = a + b; + a = b; + b = c; } + return b; +} +fn main() { + println!("Hello, world!"); + println!("Give a Number for Fibonacci Calculation ↓"); + println!("Result = {}", fibonacci(get_input())); +} diff --git a/09-prime_numbers/src/main.rs b/09-prime_numbers/src/main.rs index dcef1ee..c53ae57 100644 --- a/09-prime_numbers/src/main.rs +++ b/09-prime_numbers/src/main.rs @@ -1,65 +1,49 @@ use std::io; -fn get_input()->u32 - { - loop - { - let mut input = String::new(); - io::stdin() - .read_line(&mut input) - .expect("Failed to read line"); - match input.trim().parse() - { - Ok(num) => return num, - Err(_) => - { - println!("Expected Valid Number"); - } - }; - } +fn get_input() -> u32 { + loop { + let mut input = String::new(); + io::stdin() + .read_line(&mut input) + .expect("Failed to read line"); + match input.trim().parse() { + Ok(num) => return num, + Err(_) => { + println!("Expected Valid Number"); + } + }; } -fn prime(limit:u32)->i8 - { - if limit < 2 - { - println!("None"); - return -1; - } - println!("\t2"); - if limit == 2 - { - - return 0; - } - let mut b; - - for i in (3..=limit).step_by(2) - { - b = true; - if i%2 != 0 - { - for j in (3..=(i as f64).sqrt()as u32).step_by(2) - { - if i%j == 0 - { - //println!("{} is even", i); - b = false; - break; - } - - } - if b == true - { - println!("\t{}", i); - } - } - } +} +fn prime(limit: u32) -> i8 { + if limit < 2 { + println!("None"); + return -1; + } + println!("\t2"); + if limit == 2 { return 0; } -fn main() - { - println!("Hello, world!"); - - println!("Limit for Prime = ↓"); - prime(get_input()); + let mut b; + for i in (3..=limit).step_by(2) { + b = true; + if i % 2 != 0 { + for j in (3..=(i as f64).sqrt() as u32).step_by(2) { + if i % j == 0 { + //println!("{} is even", i); + b = false; + break; + } + } + if b == true { + println!("\t{}", i); + } + } } + return 0; +} +fn main() { + println!("Hello, world!"); + + println!("Limit for Prime = ↓"); + prime(get_input()); +} diff --git a/10-move_clone_copy/src/main.rs b/10-move_clone_copy/src/main.rs index 04c1072..37f1163 100644 --- a/10-move_clone_copy/src/main.rs +++ b/10-move_clone_copy/src/main.rs @@ -1,28 +1,26 @@ -fn main() - { - println!("Hello, world!"); +fn main() { + println!("Hello, world!"); - - //Keeps data in heap - //Information about data are in stack - //Because data can be changed in runtime + //Keeps data itself in heap for strings + //Information about data are in stack + //Because data can be changed in runtime - //"Move" like shallow copy but invalidates original - let s1 = String::from("Some Data"); - let s2 = s1; - //println!("s1 = {}", s1); s1 isn't valid - println!("s2 = {}", s2); - //"Clone" like deep copy - let s3 = String::from("Another Data"); - let s4 = s3.clone(); //Data in heap will be copied - println!("s3 = {}", s3); - println!("s4 = {}", s4); + //"Move" like shallow copy but invalidates original + let s1 = String::from("Some Data"); + let s2 = s1; + //println!("s1 = {}", s1); s1 isn't valid + println!("s2 = {}", s2); + //"Clone" like deep copy + let s3 = String::from("Another Data"); + let s4 = s3.clone(); //Data in heap will be copied + println!("s3 = {}", s3); + println!("s4 = {}", s4); - //All data are in stack - //Because size is known while compiling - //"Copy" - let x = 5; - let y = x; - println!("x = {}", x); - println!("y = {}", y); - } \ No newline at end of file + //All data are in stack + //Because size is known while compiling + //"Copy" + let x = 5; + let y = x; + println!("x = {}", x); + println!("y = {}", y); +} diff --git a/11-ownership/src/main.rs b/11-ownership/src/main.rs index 2d26a48..a709afd 100644 --- a/11-ownership/src/main.rs +++ b/11-ownership/src/main.rs @@ -1,29 +1,25 @@ -fn main() - { - println!("Hello, world!"); - - let n = 5; - let s1 = String::from("Learning Ownership"); - take_ownership(s1, n);//s1 is moved n is copied - //println!("{}", s1); It's gone - println!("Not Transferred, Just Copied = {}", n); - - let s2 = String::from("Be Right Back"); - let s3 = take_then_give_back_ownership(s2); - //println!("s2 = {}", s2); s2 is long gone - println!("s3 = {}", s3); +fn main() { + println!("Hello, world!"); - } + let n = 5; + let s1 = String::from("Learning Ownership"); + take_ownership(s1, n); //s1 is moved n is copied + //println!("{}", s1); It's gone + println!("Not Transferred, Just Copied = {}", n); -fn take_ownership(our_string:String, our_number:i32) - { - //ownership is transferred for string - println!("I got your String = {}", our_string); - println!("I got your Number = {}", our_number); - } //calls "drop" for string, so our_string is gone now + let s2 = String::from("Be Right Back"); + let s3 = take_then_give_back_ownership(s2); + //println!("s2 = {}", s2); s2 is long gone + println!("s3 = {}", s3); +} -fn take_then_give_back_ownership(our_string:String) -> String - { - println!("Now String is Here = {}", our_string); - our_string - } +fn take_ownership(our_string: String, our_number: i32) { + //ownership is transferred for string + println!("I got your String = {}", our_string); + println!("I got your Number = {}", our_number); +} //calls "drop" for string, so our_string is gone now + +fn take_then_give_back_ownership(our_string: String) -> String { + println!("Now String is Here = {}", our_string); + our_string +} diff --git a/12-references_borrowing/src/main.rs b/12-references_borrowing/src/main.rs index 4aea70c..bc3da71 100644 --- a/12-references_borrowing/src/main.rs +++ b/12-references_borrowing/src/main.rs @@ -1,45 +1,42 @@ -fn main() - { - println!("Hello, world!"); +fn main() { + println!("Hello, world!"); - //We can not have another reference, if we have a mutable one - let mut s1 = String::from("Safe"); - let r1 = &s1; - let r2 = &s1; - //If an unmutable reference will be used, we can not have mutable one - //let r3 = &mut s2; - //println!("References = {}-{}-{}", r1, r2, r3); - println!("References = {}-{}", r1, r2); - //r1 and r2 aren't going to be used, so we are free to have mutable reference - let r3 = &mut s1; - //One mutable reference will be used, we can not have another - //let r4 = &mut s2; - //println!("New References = {}-{}", r3, r4); - println!("New Mutable Reference = {}", r3); - //r3 isn't going to use anymore, we are free again to have another mutable reference - let r4 = &mut s1; - println!("New Mutable Reference Again = {}", r4); + //We can not have another reference, if we have a mutable one + let mut s1 = String::from("Safe"); + let r1 = &s1; + let r2 = &s1; + //If an immutable reference will be used, we can not have mutable one + //let r3 = &mut s2; + //println!("References = {}-{}-{}", r1, r2, r3); + println!("References = {}-{}", r1, r2); + //r1 and r2 aren't going to be used, so we are free to have mutable reference + let r3 = &mut s1; + //One mutable reference will be used, we can not have another + //let r4 = &mut s2; + //println!("New References = {}-{}", r3, r4); + println!("New Mutable Reference = {}", r3); + //r3 isn't going to use any longer, we are free again to have another mutable reference + let r4 = &mut s1; + println!("New Mutable Reference Again = {}", r4); - let mut s2 = String::from("Traveler"); - //No need to copy same values, or return anything - //Because we do not give owenership, just borrowing - borrow1(&s2); - borrow2(&mut s2); - println!("s2 = {}", s2); - //Because of unallocation after scope, this reference would be refered unallocated place - //Rust checks this, while compling to save us from runtime error - //let dangle_reference = dangling(); - } -fn borrow1(string_reference : &String) - { - println!("Length of String is = {}", string_reference.len()); - } -fn borrow2(string_reference : &mut String) - { - string_reference.push_str(" Code"); - } + let mut s2 = String::from("Traveller"); + //No need to copy same values, or return anything + //Because we do not give ownership, just borrowing + borrow1(&s2); + borrow2(&mut s2); + println!("s2 = {}", s2); + //Because of deallocation after scope, this reference would be referred unallocated place + //Rust checks this, while compiling to save us from runtime error + //let dangle_reference = dangling(); +} +fn borrow1(string_reference: &String) { + println!("Length of String is = {}", string_reference.len()); +} +fn borrow2(string_reference: &mut String) { + string_reference.push_str(" Code"); +} /*fn dangling() -> &String - { - let mut some_string = String::from("Are we dangle ?"); - &some_string - }*/ \ No newline at end of file +{ + let mut some_string = String::from("Are we dangle ?"); + &some_string +}*/ diff --git a/13-slice/src/main.rs b/13-slice/src/main.rs index 2f4a8a4..26ddd44 100644 --- a/13-slice/src/main.rs +++ b/13-slice/src/main.rs @@ -1,42 +1,38 @@ -fn main() - { - println!("Hello, world!"); - - //mutable because we will clean it later. - let mut s = String::from("Yellow Duck"); - //it's not &String, it's literal reference thanks to slice - let firts = firts_world(&s[..]); - println!("{}", firts); - s.clear(); - //s.clear function has mutable reference to truncate - //and if we print "first" after this - //we have to have mutable and immutable references - //at the same time - //it's forbidden as we've learned before - //println!("{}", firts); +fn main() { + println!("Hello, world!"); - let a = [0,1,2,3,4,5]; - let b = &a[2..4]; - println!("{}-{}", b[0], b[1]); - } - -fn firts_world(s: &str) -> &str - { - //it converts string to byte array - let bytes = s.as_bytes(); - - //iter is iteration we will learn later, even i don't know well - //enumerate returns index and reference in order as a tuple - for(i, &item) in bytes.iter().enumerate() - { - //We're trying to catch "space" to sepeterate our string - //Book says also we will learn pattern matching later - if item == b' ' - { - //when we find "space", we return first slice - return &s[0..i]; - } - } - //If we can not find any space, we return whole string as a literal - &s[..] + //mutable because we will clean it later. + let mut s = String::from("Yellow Duck"); + //it's not &String, it's literal reference thanks to slice + let first = first_world(&s[..]); + println!("{}", first); + s.clear(); + //s.clear function has mutable reference to truncate + //and if we print "first" after this + //we have to have mutable and immutable references + //at the same time + //it's forbidden as we've learned before + //println!("{}", first); + + let a = [0, 1, 2, 3, 4, 5]; + let b = &a[2..4]; + println!("{}-{}", b[0], b[1]); +} + +fn first_world(s: &str) -> &str { + //it converts string to byte array + let bytes = s.as_bytes(); + + //iter is iteration we will learn later, even i don't know well + //enumerate returns index and reference in order as a tuple + for (i, &item) in bytes.iter().enumerate() { + //We're trying to catch "space" to separate our string + //Book says also we will learn pattern matching later + if item == b' ' { + //when we find "space", we return first slice + return &s[0..i]; + } } + //If we can not find any space, we return whole string as a literal + &s[..] +} diff --git a/14-structure/src/main.rs b/14-structure/src/main.rs index dc2c1b1..0fc10d4 100644 --- a/14-structure/src/main.rs +++ b/14-structure/src/main.rs @@ -1,65 +1,73 @@ //this is how we implement struct -struct Person - { - id: u8, - tel: u8, - name: String, - alive: bool, - } +struct Person { + id: u8, + tel: u8, + name: String, + alive: bool, +} //they're tuple struct //they might seem like same but different -struct RGB(u8,u8,u8); -struct Coordinates(u8,u8,u8); +struct RGB(u8, u8, u8); +struct Coordinates(u8, u8, u8); //this is unit-like struct //i don't know what to do now, but seems like it's about "trait" struct UnitLike; -fn add_person(id:u8, tel:u8, name:String, alive:bool) ->Person - { - //if struct variables and funciton variables has same name - //no need to specify again - Person { id, tel, name, alive } +fn add_person(id: u8, tel: u8, name: String, alive: bool) -> Person { + //if struct variables and function variables has same name + //no need to specify again + Person { + id, + tel, + name, + alive, } -fn main() - { - println!("Hello, world!"); +} +fn main() { + println!("Hello, world!"); - let mut person1 = Person - { - id : 101, - tel : 111, - name : String::from("Ahmet"), - alive : false, - }; - - person1.name = String::from(person1.name+" Kaan"); - println!("{}\n{}\n{}\n{}", person1.id, person1.tel, person1.name, person1.alive); + let mut person1 = Person { + id: 101, + tel: 111, + name: String::from("Ahmet"), + alive: false, + }; - let person2 = Person - { - tel : 112, - ..person1 - }; + person1.name = String::from(person1.name + " Kaan"); + println!( + "{}\n{}\n{}\n{}", + person1.id, person1.tel, person1.name, person1.alive + ); - //person.name is going to be problem. because it's moved already - //println!("{}\n{}\n{}\n{}", person1.id, person1.tel, person1.name, person1.alive); - println!("{}\n{}\n{}\n{}", person2.id, person2.tel, person2.name, person2.alive); - - //we calls our function to assign values - let person3 = add_person(113, 114, String::from("Duck"), true); + let person2 = Person { + tel: 112, + ..person1 + }; - println!("{}\n{}\n{}\n{}", person3.id, person3.tel, person3.name, person3.alive); - - let color1 = RGB(1,2,3); - let location1 = Coordinates(4,5,6); + //person.name is going to be problem. because it's moved already + //println!("{}\n{}\n{}\n{}", person1.id, person1.tel, person1.name, person1.alive); + println!( + "{}\n{}\n{}\n{}", + person2.id, person2.tel, person2.name, person2.alive + ); - println!("{}{}{}", color1.0, color1.1, color1.2); - println!("{}{}{}", location1.0, location1.1, location1.2); - - //I don't know how to use - //that's why i used underscore to ignore compiler complaints - let _new_unit = UnitLike; + //we calls our function to assign values + let person3 = add_person(113, 114, String::from("Duck"), true); - } + println!( + "{}\n{}\n{}\n{}", + person3.id, person3.tel, person3.name, person3.alive + ); + + let colour1 = RGB(1, 2, 3); + let location1 = Coordinates(4, 5, 6); + + println!("{}{}{}", colour1.0, colour1.1, colour1.2); + println!("{}{}{}", location1.0, location1.1, location1.2); + + //I don't know how to use + //that's why i used underscore to ignore compiler complaints + let _new_unit = UnitLike; +} diff --git a/15-method/src/main.rs b/15-method/src/main.rs index 08d4488..501a29d 100644 --- a/15-method/src/main.rs +++ b/15-method/src/main.rs @@ -1,62 +1,58 @@ -const PI:f64 = 3.14; +const PI: f64 = 3.14; // we added derivation for debugging. // this trait's going to help us for printing in this code #[derive(Debug)] -struct Ellipse - { - x_radius: f64, - y_radius: f64, - } +struct Ellipse { + x_radius: f64, + y_radius: f64, +} // this implementation helps us to collect related things together // in this example all things under this will be related to Ellipse struct -// methods gets "self" as a first paramether, this is different from functions +// methods gets "self" as a first parameter, this is different from functions // functions like "circle" no need to get self as a parameter // we call them related function, they can be used as a constructor -impl Ellipse - { - fn area(&self) -> f64 - { - self.x_radius*self.y_radius*PI - } - fn x_radius(&self) -> f64 - { - self.x_radius - } - fn y_radius(&self) -> f64 - { - self.y_radius - } - fn can_fit(&self, second:&Ellipse) -> bool - { - self.x_radius >= second.x_radius && self.y_radius >= second.y_radius - } - fn circle(size:f64) -> Self - { - Self { x_radius: (size), y_radius: (size) } - } +impl Ellipse { + fn area(&self) -> f64 { + self.x_radius * self.y_radius * PI } - -fn main() - { - println!("Hello, world!"); - - let ellipse1 = Ellipse - { - x_radius : 2.0, - y_radius : 3.0, - }; - let ellipse2 = Ellipse - { - x_radius : 1.0, - y_radius : 2.0, - }; - let circle = Ellipse::circle(5.0); - println!("Ellipse is {:#?}", ellipse1); - println!("Area of the ellipse is {}", ellipse1.area()); - println!("X = {}", ellipse1.x_radius()); - println!("Y = {}", ellipse1.y_radius()); - println!("Is first be able to hug second: {}", ellipse1.can_fit(&ellipse2)); - println!("Are of new circle = {}", circle.area()); + fn x_radius(&self) -> f64 { + self.x_radius } + fn y_radius(&self) -> f64 { + self.y_radius + } + fn can_fit(&self, second: &Ellipse) -> bool { + self.x_radius >= second.x_radius && self.y_radius >= second.y_radius + } + fn circle(size: f64) -> Self { + Self { + x_radius: (size), + y_radius: (size), + } + } +} + +fn main() { + println!("Hello, world!"); + + let ellipse1 = Ellipse { + x_radius: 2.0, + y_radius: 3.0, + }; + let ellipse2 = Ellipse { + x_radius: 1.0, + y_radius: 2.0, + }; + let circle = Ellipse::circle(5.0); + println!("Ellipse is {:#?}", ellipse1); + println!("Area of the ellipse is {}", ellipse1.area()); + println!("X = {}", ellipse1.x_radius()); + println!("Y = {}", ellipse1.y_radius()); + println!( + "Is first be able to hug second: {}", + ellipse1.can_fit(&ellipse2) + ); + println!("Are of new circle = {}", circle.area()); +} diff --git a/16-enumaration/src/main.rs b/16-enumaration/src/main.rs index 1e27d0a..1f11d5d 100644 --- a/16-enumaration/src/main.rs +++ b/16-enumaration/src/main.rs @@ -1,63 +1,74 @@ -// this is kind of null implemetation +// this is kind of null implementation // but better // because we have to specify type // and compiler will make sure we can not be able to treat as a non null // is generic type parameter we will learn later // for now it can accept any type of data -// "T" can be everyting +// "T" can be everything #[derive(Debug)] -enum Option - { - None, - Some(T), - } +enum Option { + None, + Some(T), +} // this feels like event-driven programming // in this code, we basically have led states // that they have different type of associations -// On state just keeps rgb color +// On state just keeps rgb colour // Blink and Breath keeps rgb and time value // Off keeps nothing #[derive(Debug)] -enum LedState - { - On(u8,u8,u8), - Blink(u8,u8,u8,u16), - Breath(u8,u8,u8,u16), - Off, +enum LedState { + On(u8, u8, u8), + Blink(u8, u8, u8, u16), + Breath(u8, u8, u8, u16), + Off, +} +impl LedState { + // this is also method, like we did in structs + fn getter(&self) -> &Self { + self } -impl LedState - { - // this is also method, like we did in structs - fn getter(&self) -> &Self - { - self + + fn export_values(&self) -> Vec { + match self { + LedState::On(r, g, b) => vec![*r as u16, *g as u16, *b as u16], + LedState::Blink(r, g, b, effect_time) => { + vec![*r as u16, *g as u16, *b as u16, *effect_time] } + LedState::Breath(r, g, b, effect_time) => { + vec![*r as u16, *g as u16, *b as u16, *effect_time] + } + LedState::Off => vec![], + } } +} -fn main() - { - println!("Hello, world!"); +fn main() { + println!("Hello, world!"); - let green_light = LedState::On(0,255,0); - let red_breathing = LedState::Breath(255,0,0, 10); - let blue_blinking = LedState::Blink(0,0,255, 5); - let black = LedState::Off; + let green_light = LedState::On(0, 255, 0); + let red_breathing = LedState::Breath(255, 0, 0, 10); + let blue_blinking = LedState::Blink(0, 0, 255, 5); + let black = LedState::Off; - println!("State is = {:#?}", LedState::getter(&green_light)); - println!("State is = {:#?}", LedState::getter(&red_breathing)); - println!("State is = {:#?}", LedState::getter(&blue_blinking)); - println!("State is = {:#?}", LedState::getter(&black)); + println!("State is = {:#?}", LedState::getter(&green_light)); + println!("State is = {:#?}", LedState::getter(&red_breathing)); + println!("State is = {:#?}", LedState::getter(&blue_blinking)); + println!("State is = {:#?}", LedState::getter(&black)); - let null:Option = Option::None; - let two = Option::Some(2); + println!("Values Are = {:#?}", green_light.export_values()); + println!("Values Are = {:#?}", red_breathing.export_values()); + println!("Values Are = {:#?}", blue_blinking.export_values()); + println!("Values Are = {:#?}", black.export_values()); - println!("Value is = {:#?}", null); - println!("Value is = {:#?}", two); + let null: Option = Option::None; + let two = Option::Some(2); - // this is not going to work - // until we convert it - //println!("Total is = {}", 2+two); + println!("Value is = {:#?}", null); + println!("Value is = {:#?}", two); - - } + // this is not going to work + // until we convert it + // println!("Total is = {}", 2+two); +} diff --git a/17-matching/src/main.rs b/17-matching/src/main.rs index c3d7449..4a27188 100644 --- a/17-matching/src/main.rs +++ b/17-matching/src/main.rs @@ -1,49 +1,43 @@ #[derive(Debug)] -enum Fruits - { - Apple, - Banana, - Strawberry - } -enum Cake - { - Chocolate, - Caramel, - Fruit(Fruits), - Tahini, - Butter, - Vanilin, - } -fn main() - { - println!("Hello, world!"); - let cake1 = Cake::Chocolate; - let cake2 = Cake::Caramel; - let cake3 = Cake::Fruit(Fruits::Apple); - let cake4 = Cake::Fruit(Fruits::Banana); - let cake5 = Cake::Fruit(Fruits::Strawberry); - let cake6 = Cake::Tahini; - let cake7 = Cake::Butter; - let cake8 = Cake::Vanilin; - - let cakes = [cake1,cake2,cake3,cake4,cake5,cake6,cake7,cake8]; - for i in cakes - { - // our dear match always warn us to cover all posibilities - // they are like switch case but better I think - // they can be useful with enums - match i - { - Cake::Chocolate => println!("Chocolate"), - Cake::Caramel => println!("Caramel"), - Cake::Fruit(name) => println!("Fruit {:#?}", name), - Cake::Tahini => println!("Tahini"), - // this one is wildcard - // but when we don't want to use variable - // we can use it to cover all posibilities - // that's why it's the last one - _ => println!("Others!"), - } - } +enum Fruits { + Apple, + Banana, + Strawberry, +} +enum Cake { + Chocolate, + Caramel, + Fruit(Fruits), + Tahini, + Butter, + Vanillin, +} +fn main() { + println!("Hello, world!"); + let cake1 = Cake::Chocolate; + let cake2 = Cake::Caramel; + let cake3 = Cake::Fruit(Fruits::Apple); + let cake4 = Cake::Fruit(Fruits::Banana); + let cake5 = Cake::Fruit(Fruits::Strawberry); + let cake6 = Cake::Tahini; + let cake7 = Cake::Butter; + let cake8 = Cake::Vanillin; + let cakes = [cake1, cake2, cake3, cake4, cake5, cake6, cake7, cake8]; + for i in cakes { + // our dear match always warn us to cover all possibilities + // they are like switch case but better I think + // they can be useful with enums + match i { + Cake::Chocolate => println!("Chocolate"), + Cake::Caramel => println!("Caramel"), + Cake::Fruit(name) => println!("Fruit {:#?}", name), + Cake::Tahini => println!("Tahini"), + // this one is wildcard + // but when we don't want to use variable + // we can use it to cover all possibilities + // that's why it's the last one + _ => println!("Others!"), + } } +} diff --git a/18-if_let/src/main.rs b/18-if_let/src/main.rs index 58c1721..29487ee 100644 --- a/18-if_let/src/main.rs +++ b/18-if_let/src/main.rs @@ -1,37 +1,31 @@ #[derive(Debug)] -enum Vehicle - { - Car, - Bus, - Truck, - Bicycle, - Scooter, - } -fn main() - { - println!("Hello, world!"); - - let vehicle1 = Vehicle::Car; - let vehicle2 = Vehicle::Bus; - let vehicle3 = Vehicle::Truck; - let vehicle4 = Vehicle::Bicycle; - let vehicle5 = Vehicle::Scooter; - - let vehicles = [vehicle1,vehicle2,vehicle3,vehicle4,vehicle5]; - - for i in vehicles - { - // it's like match actually - // but less boilerplate - // this does not force us to cover all posibilities - // but it can, if you want - if let Vehicle::Car = i - { - println!("{:#?} is Car", i); - } - else - { - println!("{:#?} is Another Thing", i); - } - } +enum Vehicle { + Car, + Bus, + Truck, + Bicycle, + Scooter, +} +fn main() { + println!("Hello, world!"); + + let vehicle1 = Vehicle::Car; + let vehicle2 = Vehicle::Bus; + let vehicle3 = Vehicle::Truck; + let vehicle4 = Vehicle::Bicycle; + let vehicle5 = Vehicle::Scooter; + + let vehicles = [vehicle1, vehicle2, vehicle3, vehicle4, vehicle5]; + + for i in vehicles { + // it's like match actually + // but less boilerplate + // this does not force us to cover all possibilities + // but it can, if you want + if let Vehicle::Car = i { + println!("{:#?} is Car", i); + } else { + println!("{:#?} is Another Thing", i); + } } +} diff --git a/19-organizing_files/src/main.rs b/19-organizing_files/src/main.rs index 7585291..415e730 100644 --- a/19-organizing_files/src/main.rs +++ b/19-organizing_files/src/main.rs @@ -1,6 +1,6 @@ //We can call modules with use to bring them into scope -//Call them seperately +//Call them separately //use organizing_files::gift_first; //use organizing_files::gift_second;