fix: ✏️ typos and compiler warnings
This commit is contained in:
parent
82c2fb8883
commit
528969944d
17 changed files with 578 additions and 632 deletions
|
@ -1,35 +1,28 @@
|
|||
//Libs
|
||||
use std::io;
|
||||
use std::cmp::Ordering;
|
||||
use rand::Rng;
|
||||
//Funcs
|
||||
fn main()
|
||||
{
|
||||
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
|
||||
{
|
||||
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()
|
||||
{
|
||||
let guess: u32 = match guess.trim().parse() {
|
||||
Ok(num) => num,
|
||||
Err(_) => continue,
|
||||
};
|
||||
println!("You Guessed = {guess}");
|
||||
match guess.cmp(&secret_number)
|
||||
{
|
||||
match guess.cmp(&secret_number) {
|
||||
Ordering::Less => println!("Too small"),
|
||||
Ordering::Greater => println!("Too big"),
|
||||
Ordering::Equal =>
|
||||
{
|
||||
Ordering::Equal => {
|
||||
println!("You win");
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -22,7 +22,7 @@ fn main() {
|
|||
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 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}");
|
||||
|
||||
|
@ -48,13 +48,19 @@ fn main() {
|
|||
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]);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
fn main()
|
||||
{
|
||||
fn main() {
|
||||
hello();
|
||||
|
||||
let x = {
|
||||
|
@ -8,18 +7,15 @@ fn main()
|
|||
};
|
||||
println!("X = {}", x);
|
||||
|
||||
|
||||
let y = return_value(x);
|
||||
println!("Y = {}", y)
|
||||
}
|
||||
|
||||
fn hello()
|
||||
{
|
||||
fn hello() {
|
||||
println!("Hello World");
|
||||
}
|
||||
|
||||
fn return_value(mut x:i32) -> i32
|
||||
{
|
||||
fn return_value(mut x: i32) -> i32 {
|
||||
println!("We're going to return something");
|
||||
x = x + 1;
|
||||
x * x
|
||||
|
|
|
@ -3,16 +3,11 @@ fn main() {
|
|||
|
||||
let x = 3;
|
||||
|
||||
if x<0
|
||||
{
|
||||
if x < 0 {
|
||||
println!("Below Zero");
|
||||
}
|
||||
else if x==0
|
||||
{
|
||||
} else if x == 0 {
|
||||
println!("Equals Zero");
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
println!("Above Zero");
|
||||
}
|
||||
let val = true;
|
||||
|
@ -20,38 +15,34 @@ fn main() {
|
|||
|
||||
println!("X is = {x}");
|
||||
|
||||
'outer_loop: loop //Labeling a loop
|
||||
'outer_loop: loop
|
||||
//Labeling a loop
|
||||
{
|
||||
println!("Outer Loop");
|
||||
'inner_loop: loop
|
||||
{
|
||||
'inner_loop: loop {
|
||||
println!("Inner Loop");
|
||||
if x==5
|
||||
{
|
||||
if x == 5 {
|
||||
break 'outer_loop;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
break 'inner_loop;
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut y = 0;
|
||||
while y==0
|
||||
{
|
||||
while y == 0 {
|
||||
y += 1;
|
||||
}
|
||||
let q = [0, 1, 2, 3, 4];
|
||||
|
||||
for element in q //For Each
|
||||
for element in q
|
||||
//For Each
|
||||
{
|
||||
println!("Element of Array = {element}");
|
||||
}
|
||||
|
||||
for element in (0..100).rev() //Standart For
|
||||
for element in (0..100).rev()
|
||||
//Standard For
|
||||
{
|
||||
println!("Element = {element}");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -1,52 +1,45 @@
|
|||
use std::io;
|
||||
fn get_input() -> i32
|
||||
{
|
||||
loop
|
||||
{
|
||||
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()
|
||||
{
|
||||
match input.trim().parse() {
|
||||
Ok(num) => return num,
|
||||
Err(_) =>
|
||||
{
|
||||
Err(_) => {
|
||||
println!("Expected Valid Number");
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
fn main()
|
||||
{
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
|
||||
println!("1) Fahrenheit to Celsius\n2) Celsius to Fahrenheit");
|
||||
let mut selection;
|
||||
let mut b = true;
|
||||
while b
|
||||
{
|
||||
while b {
|
||||
selection = get_input();
|
||||
match selection
|
||||
{
|
||||
1 =>
|
||||
{
|
||||
match selection {
|
||||
1 => {
|
||||
println!("Fahrenheit Value = ↓");
|
||||
selection = get_input();
|
||||
println!("Celsius Value = {}", (selection - 32) as f32 * 5.0 / 9.0);
|
||||
b = false;
|
||||
},
|
||||
2 =>
|
||||
{
|
||||
}
|
||||
2 => {
|
||||
println!("Celsius Value = ↓");
|
||||
selection = get_input();
|
||||
println!("Fahrenheit Value = {}", (selection as f32*9.0/5.0)+32.0);
|
||||
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;
|
||||
fn get_input()->u32
|
||||
{
|
||||
loop
|
||||
{
|
||||
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()
|
||||
{
|
||||
match input.trim().parse() {
|
||||
Ok(num) => return num,
|
||||
Err(_) =>
|
||||
{
|
||||
Err(_) => {
|
||||
println!("Expected Valid Number");
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
fn fibo(val:u32)->u128
|
||||
{
|
||||
fn fibonacci(val: u32) -> u128 {
|
||||
let mut a = 0;
|
||||
let mut b = 1;
|
||||
let mut c;
|
||||
for _element in 2..=val
|
||||
{
|
||||
for _element in 2..=val {
|
||||
c = a + b;
|
||||
a = b;
|
||||
b = c;
|
||||
}
|
||||
return b;
|
||||
}
|
||||
fn main()
|
||||
{
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
println!("Give a Number for Fibonacci Calculation ↓");
|
||||
println!("Result = {}",fibo(get_input()));
|
||||
println!("Result = {}", fibonacci(get_input()));
|
||||
}
|
||||
|
|
|
@ -1,65 +1,49 @@
|
|||
use std::io;
|
||||
fn get_input()->u32
|
||||
{
|
||||
loop
|
||||
{
|
||||
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()
|
||||
{
|
||||
match input.trim().parse() {
|
||||
Ok(num) => return num,
|
||||
Err(_) =>
|
||||
{
|
||||
Err(_) => {
|
||||
println!("Expected Valid Number");
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
fn prime(limit:u32)->i8
|
||||
{
|
||||
if limit < 2
|
||||
{
|
||||
fn prime(limit: u32) -> i8 {
|
||||
if limit < 2 {
|
||||
println!("None");
|
||||
return -1;
|
||||
}
|
||||
println!("\t2");
|
||||
if limit == 2
|
||||
{
|
||||
|
||||
if limit == 2 {
|
||||
return 0;
|
||||
}
|
||||
let mut b;
|
||||
|
||||
for i in (3..=limit).step_by(2)
|
||||
{
|
||||
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
|
||||
{
|
||||
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
|
||||
{
|
||||
if b == true {
|
||||
println!("\t{}", i);
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
fn main()
|
||||
{
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
|
||||
println!("Limit for Prime = ↓");
|
||||
prime(get_input());
|
||||
|
||||
}
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
fn main()
|
||||
{
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
|
||||
|
||||
//Keeps data in heap
|
||||
//Keeps data itself in heap for strings
|
||||
//Information about data are in stack
|
||||
//Because data can be changed in runtime
|
||||
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
fn main()
|
||||
{
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
|
||||
let n = 5;
|
||||
|
@ -12,18 +11,15 @@ fn main()
|
|||
let s3 = take_then_give_back_ownership(s2);
|
||||
//println!("s2 = {}", s2); s2 is long gone
|
||||
println!("s3 = {}", s3);
|
||||
|
||||
}
|
||||
|
||||
fn take_ownership(our_string:String, our_number:i32)
|
||||
{
|
||||
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
|
||||
{
|
||||
fn take_then_give_back_ownership(our_string: String) -> String {
|
||||
println!("Now String is Here = {}", our_string);
|
||||
our_string
|
||||
}
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
fn main()
|
||||
{
|
||||
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
|
||||
//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);
|
||||
|
@ -16,26 +15,24 @@ fn main()
|
|||
//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
|
||||
//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");
|
||||
let mut s2 = String::from("Traveller");
|
||||
//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);
|
||||
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
|
||||
//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)
|
||||
{
|
||||
fn borrow1(string_reference: &String) {
|
||||
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");
|
||||
}
|
||||
/*fn dangling() -> &String
|
||||
|
|
|
@ -1,38 +1,34 @@
|
|||
fn main()
|
||||
{
|
||||
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);
|
||||
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!("{}", firts);
|
||||
//println!("{}", first);
|
||||
|
||||
let a = [0, 1, 2, 3, 4, 5];
|
||||
let b = &a[2..4];
|
||||
println!("{}-{}", b[0], b[1]);
|
||||
}
|
||||
|
||||
fn firts_world(s: &str) -> &str
|
||||
{
|
||||
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 sepeterate our string
|
||||
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' '
|
||||
{
|
||||
if item == b' ' {
|
||||
//when we find "space", we return first slice
|
||||
return &s[0..i];
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
//this is how we implement struct
|
||||
struct Person
|
||||
{
|
||||
struct Person {
|
||||
id: u8,
|
||||
tel: u8,
|
||||
name: String,
|
||||
|
@ -16,18 +15,20 @@ struct Coordinates(u8,u8,u8);
|
|||
//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
|
||||
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 }
|
||||
Person {
|
||||
id,
|
||||
tel,
|
||||
name,
|
||||
alive,
|
||||
}
|
||||
fn main()
|
||||
{
|
||||
}
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
|
||||
let mut person1 = Person
|
||||
{
|
||||
let mut person1 = Person {
|
||||
id: 101,
|
||||
tel: 111,
|
||||
name: String::from("Ahmet"),
|
||||
|
@ -35,31 +36,38 @@ fn main()
|
|||
};
|
||||
|
||||
person1.name = String::from(person1.name + " Kaan");
|
||||
println!("{}\n{}\n{}\n{}", person1.id, person1.tel, person1.name, person1.alive);
|
||||
println!(
|
||||
"{}\n{}\n{}\n{}",
|
||||
person1.id, person1.tel, person1.name, person1.alive
|
||||
);
|
||||
|
||||
let person2 = Person
|
||||
{
|
||||
let person2 = Person {
|
||||
tel: 112,
|
||||
..person1
|
||||
};
|
||||
|
||||
//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!(
|
||||
"{}\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);
|
||||
|
||||
println!("{}\n{}\n{}\n{}", person3.id, person3.tel, person3.name, person3.alive);
|
||||
println!(
|
||||
"{}\n{}\n{}\n{}",
|
||||
person3.id, person3.tel, person3.name, person3.alive
|
||||
);
|
||||
|
||||
let color1 = RGB(1,2,3);
|
||||
let colour1 = RGB(1, 2, 3);
|
||||
let location1 = Coordinates(4, 5, 6);
|
||||
|
||||
println!("{}{}{}", color1.0, color1.1, color1.2);
|
||||
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;
|
||||
|
||||
}
|
||||
|
|
|
@ -3,52 +3,45 @@ 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
|
||||
{
|
||||
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
|
||||
{
|
||||
impl Ellipse {
|
||||
fn area(&self) -> f64 {
|
||||
self.x_radius * self.y_radius * PI
|
||||
}
|
||||
fn x_radius(&self) -> f64
|
||||
{
|
||||
fn x_radius(&self) -> f64 {
|
||||
self.x_radius
|
||||
}
|
||||
fn y_radius(&self) -> f64
|
||||
{
|
||||
fn y_radius(&self) -> f64 {
|
||||
self.y_radius
|
||||
}
|
||||
fn can_fit(&self, second:&Ellipse) -> bool
|
||||
{
|
||||
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 circle(size: f64) -> Self {
|
||||
Self {
|
||||
x_radius: (size),
|
||||
y_radius: (size),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main()
|
||||
{
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
|
||||
let ellipse1 = Ellipse
|
||||
{
|
||||
let ellipse1 = Ellipse {
|
||||
x_radius: 2.0,
|
||||
y_radius: 3.0,
|
||||
};
|
||||
let ellipse2 = Ellipse
|
||||
{
|
||||
let ellipse2 = Ellipse {
|
||||
x_radius: 1.0,
|
||||
y_radius: 2.0,
|
||||
};
|
||||
|
@ -57,6 +50,9 @@ fn main()
|
|||
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!(
|
||||
"Is first be able to hug second: {}",
|
||||
ellipse1.can_fit(&ellipse2)
|
||||
);
|
||||
println!("Are of new circle = {}", circle.area());
|
||||
}
|
||||
|
|
|
@ -1,13 +1,12 @@
|
|||
// 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
|
||||
// <T> 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 <T>
|
||||
{
|
||||
enum Option<T> {
|
||||
None,
|
||||
Some(T),
|
||||
}
|
||||
|
@ -15,28 +14,37 @@ enum Option <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
|
||||
{
|
||||
enum LedState {
|
||||
On(u8, u8, u8),
|
||||
Blink(u8, u8, u8, u16),
|
||||
Breath(u8, u8, u8, u16),
|
||||
Off,
|
||||
}
|
||||
impl LedState
|
||||
{
|
||||
impl LedState {
|
||||
// this is also method, like we did in structs
|
||||
fn getter(&self) -> &Self
|
||||
{
|
||||
fn getter(&self) -> &Self {
|
||||
self
|
||||
}
|
||||
|
||||
fn export_values(&self) -> Vec<u16> {
|
||||
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()
|
||||
{
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
|
||||
let green_light = LedState::On(0, 255, 0);
|
||||
|
@ -49,6 +57,11 @@ fn main()
|
|||
println!("State is = {:#?}", LedState::getter(&blue_blinking));
|
||||
println!("State is = {:#?}", LedState::getter(&black));
|
||||
|
||||
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());
|
||||
|
||||
let null: Option<u8> = Option::None;
|
||||
let two = Option::Some(2);
|
||||
|
||||
|
@ -58,6 +71,4 @@ fn main()
|
|||
// this is not going to work
|
||||
// until we convert it
|
||||
// println!("Total is = {}", 2+two);
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -1,21 +1,18 @@
|
|||
#[derive(Debug)]
|
||||
enum Fruits
|
||||
{
|
||||
enum Fruits {
|
||||
Apple,
|
||||
Banana,
|
||||
Strawberry
|
||||
Strawberry,
|
||||
}
|
||||
enum Cake
|
||||
{
|
||||
enum Cake {
|
||||
Chocolate,
|
||||
Caramel,
|
||||
Fruit(Fruits),
|
||||
Tahini,
|
||||
Butter,
|
||||
Vanilin,
|
||||
Vanillin,
|
||||
}
|
||||
fn main()
|
||||
{
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
let cake1 = Cake::Chocolate;
|
||||
let cake2 = Cake::Caramel;
|
||||
|
@ -24,26 +21,23 @@ fn main()
|
|||
let cake5 = Cake::Fruit(Fruits::Strawberry);
|
||||
let cake6 = Cake::Tahini;
|
||||
let cake7 = Cake::Butter;
|
||||
let cake8 = Cake::Vanilin;
|
||||
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 posibilities
|
||||
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
|
||||
{
|
||||
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
|
||||
// we can use it to cover all possibilities
|
||||
// that's why it's the last one
|
||||
_ => println!("Others!"),
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,14 +1,12 @@
|
|||
#[derive(Debug)]
|
||||
enum Vehicle
|
||||
{
|
||||
enum Vehicle {
|
||||
Car,
|
||||
Bus,
|
||||
Truck,
|
||||
Bicycle,
|
||||
Scooter,
|
||||
}
|
||||
fn main()
|
||||
{
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
|
||||
let vehicle1 = Vehicle::Car;
|
||||
|
@ -19,18 +17,14 @@ fn main()
|
|||
|
||||
let vehicles = [vehicle1, vehicle2, vehicle3, vehicle4, vehicle5];
|
||||
|
||||
for i in vehicles
|
||||
{
|
||||
for i in vehicles {
|
||||
// it's like match actually
|
||||
// but less boilerplate
|
||||
// this does not force us to cover all posibilities
|
||||
// this does not force us to cover all possibilities
|
||||
// but it can, if you want
|
||||
if let Vehicle::Car = i
|
||||
{
|
||||
if let Vehicle::Car = i {
|
||||
println!("{:#?} is Car", i);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
println!("{:#?} is Another Thing", i);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue