summaryrefslogtreecommitdiff
path: root/rust
diff options
context:
space:
mode:
Diffstat (limited to 'rust')
-rw-r--r--rust/theBook/chapter-3-variables-and-mutability/control-if/Cargo.toml8
-rw-r--r--rust/theBook/chapter-3-variables-and-mutability/control-if/src/main.rs19
-rw-r--r--rust/theBook/chapter-3-variables-and-mutability/control-loops/Cargo.toml8
-rw-r--r--rust/theBook/chapter-3-variables-and-mutability/control-loops/src/main.rs42
-rw-r--r--rust/theBook/chapter-3-variables-and-mutability/practice/convert-temp/Cargo.toml8
-rw-r--r--rust/theBook/chapter-3-variables-and-mutability/practice/convert-temp/src/main.rs59
-rw-r--r--rust/theBook/chapter-3-variables-and-mutability/practice/fibonacci/Cargo.toml8
-rw-r--r--rust/theBook/chapter-3-variables-and-mutability/practice/fibonacci/src/main.rs67
-rw-r--r--rust/theBook/chapter-3-variables-and-mutability/variables/Cargo.toml8
-rw-r--r--rust/theBook/chapter-3-variables-and-mutability/variables/src/main.rs7
10 files changed, 234 insertions, 0 deletions
diff --git a/rust/theBook/chapter-3-variables-and-mutability/control-if/Cargo.toml b/rust/theBook/chapter-3-variables-and-mutability/control-if/Cargo.toml
new file mode 100644
index 0000000..141fef0
--- /dev/null
+++ b/rust/theBook/chapter-3-variables-and-mutability/control-if/Cargo.toml
@@ -0,0 +1,8 @@
+[package]
+name = "control-if"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
diff --git a/rust/theBook/chapter-3-variables-and-mutability/control-if/src/main.rs b/rust/theBook/chapter-3-variables-and-mutability/control-if/src/main.rs
new file mode 100644
index 0000000..8866444
--- /dev/null
+++ b/rust/theBook/chapter-3-variables-and-mutability/control-if/src/main.rs
@@ -0,0 +1,19 @@
+fn main() {
+ let number = 3;
+
+ if number < 5 {
+ println!("number is less than 5");
+ } else {
+ println!("number is bigger than 5");
+ }
+
+ let use_let = if number < 5 {number} else {5};
+
+ println!("number is {use_let}");
+
+ println!("number is {}", if number < 5 {4} else {6});
+
+ let names = ["name", "is", "idiot"];
+
+ println!("names: {:?}",names);
+}
diff --git a/rust/theBook/chapter-3-variables-and-mutability/control-loops/Cargo.toml b/rust/theBook/chapter-3-variables-and-mutability/control-loops/Cargo.toml
new file mode 100644
index 0000000..e19b1fa
--- /dev/null
+++ b/rust/theBook/chapter-3-variables-and-mutability/control-loops/Cargo.toml
@@ -0,0 +1,8 @@
+[package]
+name = "control-loops"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
diff --git a/rust/theBook/chapter-3-variables-and-mutability/control-loops/src/main.rs b/rust/theBook/chapter-3-variables-and-mutability/control-loops/src/main.rs
new file mode 100644
index 0000000..e0e6f4c
--- /dev/null
+++ b/rust/theBook/chapter-3-variables-and-mutability/control-loops/src/main.rs
@@ -0,0 +1,42 @@
+fn main() {
+ let mut counter = 0;
+
+ let result = loop {
+ counter += 1;
+
+ if counter == 10 {
+ break counter * 2;
+ }
+ };
+
+ println!("the result is {result}");
+
+ let mut count = 0;
+
+ 'counting_up: loop {
+ println!("count = {count}");
+ let mut remaining = 10;
+
+ loop {
+ println!("remaining = {remaining}");
+
+ if remaining == 9 {
+ break;
+ }
+ if count == 2 {
+ break 'counting_up;
+ }
+ remaining -= 1;
+ }
+
+ count += 1;
+ }
+ println!("count = {count}");
+
+ let mura = [1,2,3,4,5];
+
+ for a in mura.into_iter() {
+ println!("{a}");
+ }
+ println!("{:?}",mura);
+}
diff --git a/rust/theBook/chapter-3-variables-and-mutability/practice/convert-temp/Cargo.toml b/rust/theBook/chapter-3-variables-and-mutability/practice/convert-temp/Cargo.toml
new file mode 100644
index 0000000..391d95c
--- /dev/null
+++ b/rust/theBook/chapter-3-variables-and-mutability/practice/convert-temp/Cargo.toml
@@ -0,0 +1,8 @@
+[package]
+name = "convert-temp"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
diff --git a/rust/theBook/chapter-3-variables-and-mutability/practice/convert-temp/src/main.rs b/rust/theBook/chapter-3-variables-and-mutability/practice/convert-temp/src/main.rs
new file mode 100644
index 0000000..971811e
--- /dev/null
+++ b/rust/theBook/chapter-3-variables-and-mutability/practice/convert-temp/src/main.rs
@@ -0,0 +1,59 @@
+use std::io;
+use std::process;
+
+fn read_input() -> i32 {
+ let mut input = String::new();
+
+ io::stdin()
+ .read_line(&mut input)
+ .expect("failed to read line");
+
+ let input = match input.trim().parse() {
+ Ok(input) => input,
+ Err(_) => {
+ println!("not a number!");
+ process::exit(1);
+ },
+ };
+ input
+}
+
+fn main() {
+ println!("Convert temperatures between Fahrenheit and Celsius");
+ println!("");
+
+ loop {
+ println!("Please choose converting method:");
+ println!("1) Fahrenheit to Celsius 2) Celsius to Fahrenheit");
+ println!("3) exit");
+
+ let choice = read_input();
+
+ if choice == 0 {
+ let _choice = read_input();
+ } else if choice == 1 {
+ fah_to_cel();
+ } else if choice == 2 {
+ cel_to_fah();
+ } else if choice == 3 {
+ break
+ };
+ }
+ println!("Thinks for using");
+}
+
+fn fah_to_cel() {
+ println!("Please enter a Fahrenheit temperature");
+ let fah = read_input() as f32;
+
+ println!("the Celsius of {fah} is {}", (fah-32.0)/1.8);
+ println!("");
+}
+
+fn cel_to_fah() {
+ println!("Please enter a Celsius temperature");
+ let cel = read_input() as f32;
+
+ println!("The Fahrenteit of {cel} is {}", cel*1.8+32.0);
+ println!("");
+}
diff --git a/rust/theBook/chapter-3-variables-and-mutability/practice/fibonacci/Cargo.toml b/rust/theBook/chapter-3-variables-and-mutability/practice/fibonacci/Cargo.toml
new file mode 100644
index 0000000..9cd048e
--- /dev/null
+++ b/rust/theBook/chapter-3-variables-and-mutability/practice/fibonacci/Cargo.toml
@@ -0,0 +1,8 @@
+[package]
+name = "fibonacci"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
diff --git a/rust/theBook/chapter-3-variables-and-mutability/practice/fibonacci/src/main.rs b/rust/theBook/chapter-3-variables-and-mutability/practice/fibonacci/src/main.rs
new file mode 100644
index 0000000..078f729
--- /dev/null
+++ b/rust/theBook/chapter-3-variables-and-mutability/practice/fibonacci/src/main.rs
@@ -0,0 +1,67 @@
+use std::io;
+use std::process;
+
+fn main() {
+ println!("Print nth Fibonacci number!");
+ println!("");
+
+ print_fibonacci();
+
+ println!("done!");
+}
+
+fn print_fibonacci() {
+ println!("Enter the n you want to print");
+
+ let fib = read_input();
+
+ println!("recursion_fibonacci:");
+ println!("{}", recursion_fibonacci(fib));
+ loop_fibonacci(fib);
+}
+
+fn loop_fibonacci(x: i32) {
+ let mut num1 = 0;
+ let mut num2 = 1;
+ let mut next: i32;
+
+ println!("loop_fibonacci:");
+
+ for _i in 0..x {
+ if _i <= 1 {
+ next = _i;
+ } else {
+ next = num1 + num2;
+ num1 = num2;
+ num2 = next;
+ }
+ println!("{next}");
+ }
+}
+
+// Not recommend in rust
+fn recursion_fibonacci(x: i32) -> i32 {
+ if x <= 1 {
+ return x;
+ } else {
+ recursion_fibonacci(x-1) + recursion_fibonacci(x-2)
+ }
+}
+
+fn read_input() -> i32 {
+ let mut inp = String::new();
+
+ io::stdin()
+ .read_line(&mut inp)
+ .expect("failed to read number");
+
+ let inp: i32 = match inp.trim().parse() {
+ Ok(inp) => inp,
+ Err(_) => {
+ println!("converting error");
+ process::exit(1);
+ },
+ };
+
+ inp
+}
diff --git a/rust/theBook/chapter-3-variables-and-mutability/variables/Cargo.toml b/rust/theBook/chapter-3-variables-and-mutability/variables/Cargo.toml
new file mode 100644
index 0000000..f1cad76
--- /dev/null
+++ b/rust/theBook/chapter-3-variables-and-mutability/variables/Cargo.toml
@@ -0,0 +1,8 @@
+[package]
+name = "variables"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
diff --git a/rust/theBook/chapter-3-variables-and-mutability/variables/src/main.rs b/rust/theBook/chapter-3-variables-and-mutability/variables/src/main.rs
new file mode 100644
index 0000000..ecaf28f
--- /dev/null
+++ b/rust/theBook/chapter-3-variables-and-mutability/variables/src/main.rs
@@ -0,0 +1,7 @@
+fn main() {
+ let mut x = 5;
+ println!("The value of x = {x}");
+
+ x = 6;
+ println!("The value of x = {x}");
+}