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