From 33467aee8278bf66e24199fb9265a36a891fa431 Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 7 Nov 2021 12:59:15 +0530 Subject: [PATCH 01/40] strings: README added Illustration format macro --- strings/README.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/strings/README.md b/strings/README.md index be976d3..0247d8d 100644 --- a/strings/README.md +++ b/strings/README.md @@ -14,7 +14,7 @@ fn main() { } ``` -- illustration +- Illustration ```rust fn main() { /* store names inside name */ @@ -26,6 +26,18 @@ fn main() { } ``` +- Illustration: Format macro +```rust +fn main() { + let n1 = "Name".to_string(); + let n2 = "One".to_string(); + + let n3 = format!("{} {}", n1, n2); + println!("{}", n3); +} +``` + + ``` $ cargo run ``` \ No newline at end of file From 1380ecc021b07ef51e6bf48e78aaebd5c03c6ecf Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 7 Nov 2021 13:01:39 +0530 Subject: [PATCH 02/40] loops --- .github/workflows/rust.yml | 10 ++++++++++ loops/Cargo.toml | 8 ++++++++ loops/src/main.rs | 3 +++ 3 files changed, 21 insertions(+) create mode 100644 loops/Cargo.toml create mode 100644 loops/src/main.rs diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index dd0608a..12833af 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -62,4 +62,14 @@ jobs: - name: Test strings run: | cd strings/ + cargo test --verbose + + - name: Build loops + run: | + cd loops/ + cargo build --verbose + + - name: Test loops + run: | + cd loops/ cargo test --verbose \ No newline at end of file diff --git a/loops/Cargo.toml b/loops/Cargo.toml new file mode 100644 index 0000000..0d5dad5 --- /dev/null +++ b/loops/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "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/loops/src/main.rs b/loops/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/loops/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} From 5da2708c0465d96f454e5a086adf6b403a0fd0f0 Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 7 Nov 2021 13:04:00 +0530 Subject: [PATCH 03/40] loops --- loops/.gitignore | 10 ++++++++++ loops/src/main.rs | 9 +++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 loops/.gitignore diff --git a/loops/.gitignore b/loops/.gitignore new file mode 100644 index 0000000..088ba6b --- /dev/null +++ b/loops/.gitignore @@ -0,0 +1,10 @@ +# Generated by Cargo +# will have compiled files and executables +/target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk diff --git a/loops/src/main.rs b/loops/src/main.rs index e7a11a9..2b616d0 100644 --- a/loops/src/main.rs +++ b/loops/src/main.rs @@ -1,3 +1,8 @@ fn main() { - println!("Hello, world!"); -} + for x in 1..11 { + if x == 2 { + continue; + } + println!("x is {}", x); + } +} \ No newline at end of file From 9c9a5b9be1481883c2964f141b5edc3a7a5f3fa3 Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 7 Nov 2021 13:07:21 +0530 Subject: [PATCH 04/40] loops: README --- loops/README.md | 52 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 loops/README.md diff --git a/loops/README.md b/loops/README.md new file mode 100644 index 0000000..0f53fc5 --- /dev/null +++ b/loops/README.md @@ -0,0 +1,52 @@ +## Loops: + +- basic for loop: +```rust +fn main(){ + for x in 1..11{ + if x==5 { + continue; + } + println!("x is {}",x); + } +} +``` + +- advance loop +```rust +fn main() { + let mut x = 0; + + loop { + x += 1; + println!("x={}", x); + + if x == 15 { + break; + } + } +} +``` + +- output: +``` +x=1 +x=2 +x=3 +x=4 +x=5 +x=6 +x=7 +x=8 +x=9 +x=10 +x=11 +x=12 +x=13 +x=14 +x=15 +``` + +``` +$ cargo run +``` \ No newline at end of file From fada7e10a29924bafe39987189c920c83998593b Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 7 Nov 2021 13:14:27 +0530 Subject: [PATCH 05/40] functions + workflow --- .github/workflows/rust.yml | 10 ++++++++++ functions/.gitignore | 10 ++++++++++ functions/Cargo.toml | 8 ++++++++ functions/README.md | 1 + functions/src/main.rs | 13 +++++++++++++ 5 files changed, 42 insertions(+) create mode 100644 functions/.gitignore create mode 100644 functions/Cargo.toml create mode 100644 functions/README.md create mode 100644 functions/src/main.rs diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 12833af..ac7736d 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -72,4 +72,14 @@ jobs: - name: Test loops run: | cd loops/ + cargo test --verbose + + - name: Build functions + run: | + cd functions/ + cargo build --verbose + + - name: Test functions + run: | + cd functions/ cargo test --verbose \ No newline at end of file diff --git a/functions/.gitignore b/functions/.gitignore new file mode 100644 index 0000000..088ba6b --- /dev/null +++ b/functions/.gitignore @@ -0,0 +1,10 @@ +# Generated by Cargo +# will have compiled files and executables +/target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk diff --git a/functions/Cargo.toml b/functions/Cargo.toml new file mode 100644 index 0000000..a9b5578 --- /dev/null +++ b/functions/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "functions" +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/functions/README.md b/functions/README.md new file mode 100644 index 0000000..fd81f0d --- /dev/null +++ b/functions/README.md @@ -0,0 +1 @@ +## Functions: diff --git a/functions/src/main.rs b/functions/src/main.rs new file mode 100644 index 0000000..e58e098 --- /dev/null +++ b/functions/src/main.rs @@ -0,0 +1,13 @@ +fn main() { + printhello(); + + printhi(); +} + +fn printhello() { + println!("Hello World"); +} + +fn printhi() { + println!("HI"); +} \ No newline at end of file From 76b4e129140af774dbdce17d82f7c5e41e50f383 Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 7 Nov 2021 13:16:22 +0530 Subject: [PATCH 06/40] functions: README --- functions/README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/functions/README.md b/functions/README.md index fd81f0d..f793d66 100644 --- a/functions/README.md +++ b/functions/README.md @@ -1 +1,26 @@ ## Functions: + +- main.rs: + +- basic functions: +```rust +fn main() { + helloworld(); +} + +fn helloworld() { + println!("Hello, World"); +} +``` + +- passing strings to a func +```rust +fn main() { + let name:String = String::from("NameOne"); + display(name); +} + +fn display(param_name:String) { + println!("Name: {}", param_name); +} +``` \ No newline at end of file From f2e363502da5c3bf7d0c07d4989c1742ffe0b463 Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 7 Nov 2021 13:17:03 +0530 Subject: [PATCH 07/40] README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 4326064..d6fc8c3 100644 --- a/README.md +++ b/README.md @@ -6,5 +6,6 @@ A repo for learning rust - [`variables`](variables) variables!!! - [`consts`](consts) consts and strings - [`strings`](strings) get to know what is strings and how to use it +- [`functions`](functions) what is functions? get to know - learnrust is licensed under [`Apache-2.0 License`](LICENSE) \ No newline at end of file From 9c077558fe69ba163fb8f9021cb18d7b6fe7b274 Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 7 Nov 2021 13:21:43 +0530 Subject: [PATCH 08/40] README --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index d6fc8c3..c1ac190 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # learnrust A repo for learning rust +## Topics: - [`print`](print) basic print function - [`datatypes`](datatypes) get to know what is datatypes - [`variables`](variables) variables!!! @@ -8,4 +9,8 @@ A repo for learning rust - [`strings`](strings) get to know what is strings and how to use it - [`functions`](functions) what is functions? get to know +## Contributors: +- [krishpranav](https://github.com/krishpranav) + +## Licensing - learnrust is licensed under [`Apache-2.0 License`](LICENSE) \ No newline at end of file From 91ad14f405b73befbcf484f85e6237361ac9d858 Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 7 Nov 2021 13:24:12 +0530 Subject: [PATCH 09/40] arrays + github workflow --- .github/workflows/rust.yml | 10 ++++++++++ arrays/Cargo.toml | 8 ++++++++ arrays/src/main.rs | 3 +++ 3 files changed, 21 insertions(+) create mode 100644 arrays/Cargo.toml create mode 100644 arrays/src/main.rs diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index ac7736d..419edca 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -82,4 +82,14 @@ jobs: - name: Test functions run: | cd functions/ + cargo test --verbose + + - name: Build arrays + run: | + cd arrays/ + cargo build --verbose + + - name: Test arrays + run: | + cd arrays/ cargo test --verbose \ No newline at end of file diff --git a/arrays/Cargo.toml b/arrays/Cargo.toml new file mode 100644 index 0000000..0bcfd89 --- /dev/null +++ b/arrays/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "arrays" +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/arrays/src/main.rs b/arrays/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/arrays/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} From 1f01d7a87f0aed5e3e99122b867b6efd0a3df9b0 Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 7 Nov 2021 13:26:25 +0530 Subject: [PATCH 10/40] arrays --- arrays/.gitignore | 10 ++++++++++ arrays/README.md | 1 + arrays/src/main.rs | 5 +++-- 3 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 arrays/.gitignore create mode 100644 arrays/README.md diff --git a/arrays/.gitignore b/arrays/.gitignore new file mode 100644 index 0000000..088ba6b --- /dev/null +++ b/arrays/.gitignore @@ -0,0 +1,10 @@ +# Generated by Cargo +# will have compiled files and executables +/target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk diff --git a/arrays/README.md b/arrays/README.md new file mode 100644 index 0000000..e8b657b --- /dev/null +++ b/arrays/README.md @@ -0,0 +1 @@ +## Arrays: diff --git a/arrays/src/main.rs b/arrays/src/main.rs index e7a11a9..46618d5 100644 --- a/arrays/src/main.rs +++ b/arrays/src/main.rs @@ -1,3 +1,4 @@ fn main() { - println!("Hello, world!"); -} + let array:[i32;4] = [10, 20, 30, 40]; + println!("array is {:?}", array); +} \ No newline at end of file From 7c90762ecd8eb9a903e31481206c420c23548a55 Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 7 Nov 2021 13:27:45 +0530 Subject: [PATCH 11/40] arrays: README --- arrays/README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/arrays/README.md b/arrays/README.md index e8b657b..f0d28bf 100644 --- a/arrays/README.md +++ b/arrays/README.md @@ -1 +1,23 @@ ## Arrays: + +- main.rs + +- Simple array: +```rust +fn main() { + let array:[i32;4] = [10, 20, 30, 40]; + println!("array is {:?}", array); +} +``` + +- Array length +```rust +fn main() { + let array:[i32;4] = [10, 20, 30, 40]; + println!("array is {:?}", array.len()); +} +``` + +``` +$ cargo run +``` \ No newline at end of file From 0c5f428249ef6e59b36fc1f354af1bfb499e99f9 Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 7 Nov 2021 13:28:07 +0530 Subject: [PATCH 12/40] README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index c1ac190..b2bbacf 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ A repo for learning rust - [`consts`](consts) consts and strings - [`strings`](strings) get to know what is strings and how to use it - [`functions`](functions) what is functions? get to know +- [`arrays`](arrays) arrays ## Contributors: - [krishpranav](https://github.com/krishpranav) From 622d4cc9bbf5a8357c627d8db187fc38c3f1d3e9 Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 7 Nov 2021 13:28:57 +0530 Subject: [PATCH 13/40] arrays: README --- arrays/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/arrays/README.md b/arrays/README.md index f0d28bf..0ae0990 100644 --- a/arrays/README.md +++ b/arrays/README.md @@ -14,6 +14,7 @@ fn main() { ```rust fn main() { let array:[i32;4] = [10, 20, 30, 40]; + println!("array is {:?}", array); println!("array is {:?}", array.len()); } ``` From a1fc6cb62729e12be916c7e527593120930774fd Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 7 Nov 2021 13:31:43 +0530 Subject: [PATCH 14/40] ownertypes --- .github/workflows/rust.yml | 10 ++++++++++ ownertypes/Cargo.toml | 8 ++++++++ ownertypes/src/main.rs | 3 +++ 3 files changed, 21 insertions(+) create mode 100644 ownertypes/Cargo.toml create mode 100644 ownertypes/src/main.rs diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 419edca..b78af64 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -92,4 +92,14 @@ jobs: - name: Test arrays run: | cd arrays/ + cargo test --verbose + + - name: Build ownertypes + run: | + cd ownertypes + cargo build --verbose + + - name: Test ownertypes + run: | + cd ownertypes cargo test --verbose \ No newline at end of file diff --git a/ownertypes/Cargo.toml b/ownertypes/Cargo.toml new file mode 100644 index 0000000..7e873dd --- /dev/null +++ b/ownertypes/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "ownertypes" +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/ownertypes/src/main.rs b/ownertypes/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/ownertypes/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} From d7ec26efbc3c883759a7ddf0423a45ae1901a4f7 Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 7 Nov 2021 13:33:33 +0530 Subject: [PATCH 15/40] ownertypes --- ownertypes/.gitignore | 10 ++++++++++ ownertypes/README.md | 29 +++++++++++++++++++++++++++++ ownertypes/src/main.rs | 9 ++++++--- 3 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 ownertypes/.gitignore create mode 100644 ownertypes/README.md diff --git a/ownertypes/.gitignore b/ownertypes/.gitignore new file mode 100644 index 0000000..088ba6b --- /dev/null +++ b/ownertypes/.gitignore @@ -0,0 +1,10 @@ +# Generated by Cargo +# will have compiled files and executables +/target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk diff --git a/ownertypes/README.md b/ownertypes/README.md new file mode 100644 index 0000000..b447fd8 --- /dev/null +++ b/ownertypes/README.md @@ -0,0 +1,29 @@ +## Ownertypes: + +- main.rs + +- basic ownership: +```rust +fn main() { + let v1 = vec![1, 2, 3]; + + let v2 = v; + + println("{:?}", v); +} +``` + +- advanced ownerships: +```rust +fn main(){ + let u1 = 10; + let u2 = u1; + + println!("u1 = {}",u1); +} +``` + + +``` +$ cargo run +``` \ No newline at end of file diff --git a/ownertypes/src/main.rs b/ownertypes/src/main.rs index e7a11a9..7f5590f 100644 --- a/ownertypes/src/main.rs +++ b/ownertypes/src/main.rs @@ -1,3 +1,6 @@ -fn main() { - println!("Hello, world!"); -} +fn main(){ + let u1 = 10; + let u2 = u1; + + println!("u1 = {}",u1); + } \ No newline at end of file From aa101faa8d759af4c9fd1df15adb428ca37f1e3e Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 7 Nov 2021 13:34:46 +0530 Subject: [PATCH 16/40] ownerships --- .github/workflows/rust.yml | 8 ++++---- README.md | 2 ++ {ownertypes => ownerships}/.gitignore | 0 {ownertypes => ownerships}/Cargo.toml | 2 +- {ownertypes => ownerships}/README.md | 2 +- {ownertypes => ownerships}/src/main.rs | 0 6 files changed, 8 insertions(+), 6 deletions(-) rename {ownertypes => ownerships}/.gitignore (100%) rename {ownertypes => ownerships}/Cargo.toml (88%) rename {ownertypes => ownerships}/README.md (94%) rename {ownertypes => ownerships}/src/main.rs (100%) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index b78af64..d23557d 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -94,12 +94,12 @@ jobs: cd arrays/ cargo test --verbose - - name: Build ownertypes + - name: Build ownerships run: | - cd ownertypes + cd ownerships/ cargo build --verbose - - name: Test ownertypes + - name: Test ownerships run: | - cd ownertypes + cd ownerships/ cargo test --verbose \ No newline at end of file diff --git a/README.md b/README.md index b2bbacf..ba8535b 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,8 @@ A repo for learning rust - [`strings`](strings) get to know what is strings and how to use it - [`functions`](functions) what is functions? get to know - [`arrays`](arrays) arrays +- [`ownerships`](arrays) arrays + ## Contributors: - [krishpranav](https://github.com/krishpranav) diff --git a/ownertypes/.gitignore b/ownerships/.gitignore similarity index 100% rename from ownertypes/.gitignore rename to ownerships/.gitignore diff --git a/ownertypes/Cargo.toml b/ownerships/Cargo.toml similarity index 88% rename from ownertypes/Cargo.toml rename to ownerships/Cargo.toml index 7e873dd..f506749 100644 --- a/ownertypes/Cargo.toml +++ b/ownerships/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "ownertypes" +name = "ownerships" version = "0.1.0" edition = "2021" diff --git a/ownertypes/README.md b/ownerships/README.md similarity index 94% rename from ownertypes/README.md rename to ownerships/README.md index b447fd8..ae26c40 100644 --- a/ownertypes/README.md +++ b/ownerships/README.md @@ -1,4 +1,4 @@ -## Ownertypes: +## Ownerships: - main.rs diff --git a/ownertypes/src/main.rs b/ownerships/src/main.rs similarity index 100% rename from ownertypes/src/main.rs rename to ownerships/src/main.rs From a8148da97c016e6e321ad4e1cf5ba34cc996e7de Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 7 Nov 2021 13:39:46 +0530 Subject: [PATCH 17/40] enums + github workflow --- .github/workflows/rust.yml | 10 ++++++++++ enums/.gitignore | 10 ++++++++++ enums/Cargo.toml | 8 ++++++++ enums/README.md | 23 +++++++++++++++++++++++ enums/src/main.rs | 3 +++ 5 files changed, 54 insertions(+) create mode 100644 enums/.gitignore create mode 100644 enums/Cargo.toml create mode 100644 enums/README.md create mode 100644 enums/src/main.rs diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index d23557d..e2170b1 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -102,4 +102,14 @@ jobs: - name: Test ownerships run: | cd ownerships/ + cargo test --verbose + + - name: Build enums + run: | + cd enums/ + cargo build --verbose + + - name: Test enums + run: | + cd enums/ cargo test --verbose \ No newline at end of file diff --git a/enums/.gitignore b/enums/.gitignore new file mode 100644 index 0000000..088ba6b --- /dev/null +++ b/enums/.gitignore @@ -0,0 +1,10 @@ +# Generated by Cargo +# will have compiled files and executables +/target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk diff --git a/enums/Cargo.toml b/enums/Cargo.toml new file mode 100644 index 0000000..7b703c4 --- /dev/null +++ b/enums/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "enums" +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/enums/README.md b/enums/README.md new file mode 100644 index 0000000..8146ffc --- /dev/null +++ b/enums/README.md @@ -0,0 +1,23 @@ +## Enums: + +- main.rs: + +- basic enum +```rust +enum CarType { + SUV, + Sedan, +} + +fn main() { + let suv = CarType::SUV; + let sedan = CarType::Sedan; + + println!("{:?}", suv); + println!("{:?}", sedan); +} +``` + +``` +$ cargo run +``` \ No newline at end of file diff --git a/enums/src/main.rs b/enums/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/enums/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} From 4304df04a307b52bfc95f3e367a0fa9a1a8923a2 Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 7 Nov 2021 13:49:05 +0530 Subject: [PATCH 18/40] enums: enum funcs --- enums/README.md | 31 +++++++++++++++++++++++++++++++ enums/src/main.rs | 23 +++++++++++++++++++++-- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/enums/README.md b/enums/README.md index 8146ffc..9b544a5 100644 --- a/enums/README.md +++ b/enums/README.md @@ -4,11 +4,13 @@ - basic enum ```rust +/* storing car types */ enum CarType { SUV, Sedan, } +/* printing it out */ fn main() { let suv = CarType::SUV; let sedan = CarType::Sedan; @@ -18,6 +20,35 @@ fn main() { } ``` +- advanced: +```rust +/* storing car types */ +enum CarType { + SUV, + Sedan +} + +/* matching the cars */ +fn print_cars(car: CarType) { + match car { + + CarType::Sedan => { + println!("Sedan: Medium Sized Car!!"); + }, + + CarType::SUV => { + println!("SUV: Big Sized Car!"); + }, + } +} + +/* printing it out */ +fn main() { + print_cars(CarType::Sedan); + print_cars(CarType::SUV); +} +``` + ``` $ cargo run ``` \ No newline at end of file diff --git a/enums/src/main.rs b/enums/src/main.rs index e7a11a9..4106088 100644 --- a/enums/src/main.rs +++ b/enums/src/main.rs @@ -1,3 +1,22 @@ -fn main() { - println!("Hello, world!"); +enum CarType { + SUV, + Sedan +} + +fn print_cars(car: CarType) { + match car { + + CarType::Sedan => { + println!("Sedan: Medium Sized Car!!"); + }, + + CarType::SUV => { + println!("SUV: Big Sized Car!"); + }, + } } + +fn main() { + print_cars(CarType::Sedan); + print_cars(CarType::SUV); +} \ No newline at end of file From 1dea1a94f4a9ff2442a7d4e2a5d94a3b3d22460c Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 7 Nov 2021 13:49:38 +0530 Subject: [PATCH 19/40] README --- README.md | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index ba8535b..3a86aff 100644 --- a/README.md +++ b/README.md @@ -2,14 +2,15 @@ A repo for learning rust ## Topics: -- [`print`](print) basic print function -- [`datatypes`](datatypes) get to know what is datatypes -- [`variables`](variables) variables!!! -- [`consts`](consts) consts and strings -- [`strings`](strings) get to know what is strings and how to use it -- [`functions`](functions) what is functions? get to know -- [`arrays`](arrays) arrays -- [`ownerships`](arrays) arrays +- [`print`](print) basic print function. +- [`datatypes`](datatypes) get to know what is datatypes. +- [`variables`](variables) variables!!!. +- [`consts`](consts) consts and strings. +- [`strings`](strings) get to know what is strings and how to use it. +- [`functions`](functions) what is functions? get to know. +- [`arrays`](arrays) arrays. +- [`ownerships`](arrays) arrays. +- [`enums`](enums) get to know what is enums and how to use it. ## Contributors: From 477ad8a996a222b3c1d1493b160fab2b53897eec Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 7 Nov 2021 13:55:43 +0530 Subject: [PATCH 20/40] vectors + github workflow --- .github/workflows/rust.yml | 10 ++++++++++ vectors/.gitignore | 10 ++++++++++ vectors/Cargo.toml | 8 ++++++++ vectors/README.md | 1 + vectors/src/main.rs | 3 +++ 5 files changed, 32 insertions(+) create mode 100644 vectors/.gitignore create mode 100644 vectors/Cargo.toml create mode 100644 vectors/README.md create mode 100644 vectors/src/main.rs diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index e2170b1..c4af504 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -112,4 +112,14 @@ jobs: - name: Test enums run: | cd enums/ + cargo test --verbose + + - name: Build vectors + run: | + cd vectors/ + cargo build --verbose + + - name: Test vectors + run: | + cd vectors/ cargo test --verbose \ No newline at end of file diff --git a/vectors/.gitignore b/vectors/.gitignore new file mode 100644 index 0000000..088ba6b --- /dev/null +++ b/vectors/.gitignore @@ -0,0 +1,10 @@ +# Generated by Cargo +# will have compiled files and executables +/target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk diff --git a/vectors/Cargo.toml b/vectors/Cargo.toml new file mode 100644 index 0000000..b08136f --- /dev/null +++ b/vectors/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "vectors" +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/vectors/README.md b/vectors/README.md new file mode 100644 index 0000000..97a09b2 --- /dev/null +++ b/vectors/README.md @@ -0,0 +1 @@ +## Vectors \ No newline at end of file diff --git a/vectors/src/main.rs b/vectors/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/vectors/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} From 752aac0d1d93c7d1024b31f1ef95bcfa5bb619b3 Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 7 Nov 2021 14:00:03 +0530 Subject: [PATCH 21/40] vectors --- vectors/README.md | 35 ++++++++++++++++++++++++++++++++++- vectors/src/main.rs | 14 ++++++++++++-- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/vectors/README.md b/vectors/README.md index 97a09b2..6477bdf 100644 --- a/vectors/README.md +++ b/vectors/README.md @@ -1 +1,34 @@ -## Vectors \ No newline at end of file +## Vectors + +- basic vectors: +```rust +fn main() { + let mut v = Vec::new(); + v.push(20); + v.push(30); + v.push(40); + v.push(500); + + for i in &v { + println!("{}",i); + } + println!("{:?}",v); +} +``` + +- advanced vectors: +```rust +use std::collections::HashSet; + +fn main() { + let mut names = HashSet::new(); + + names.insert("NameOne"); + names.insert("NameTwo"); + names.insert("NameThree"); + + if names.contains(&"NameOne") { + println!("Name One Founded!"); + } +} +``` \ No newline at end of file diff --git a/vectors/src/main.rs b/vectors/src/main.rs index e7a11a9..ae5cdf8 100644 --- a/vectors/src/main.rs +++ b/vectors/src/main.rs @@ -1,3 +1,13 @@ +use std::collections::HashSet; + fn main() { - println!("Hello, world!"); -} + let mut names = HashSet::new(); + + names.insert("NameOne"); + names.insert("NameTwo"); + names.insert("NameThree"); + + if names.contains(&"NameOne") { + println!("Name One Founded!"); + } +} \ No newline at end of file From ff531684776383c398110ee168c3cc026477259a Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 7 Nov 2021 14:00:21 +0530 Subject: [PATCH 22/40] README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 3a86aff..730e00a 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ A repo for learning rust - [`arrays`](arrays) arrays. - [`ownerships`](arrays) arrays. - [`enums`](enums) get to know what is enums and how to use it. +- [`vectors`](vectors) vectors ## Contributors: From f941bba1798e8c6a5c8aab282bf5021eafad2d88 Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 7 Nov 2021 16:33:42 +0530 Subject: [PATCH 23/40] errorhandling + github workflow --- .github/workflows/rust.yml | 10 ++++++++++ README.md | 1 + errorhandling/Cargo.toml | 8 ++++++++ errorhandling/src/main.rs | 3 +++ 4 files changed, 22 insertions(+) create mode 100644 errorhandling/Cargo.toml create mode 100644 errorhandling/src/main.rs diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index c4af504..7161286 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -122,4 +122,14 @@ jobs: - name: Test vectors run: | cd vectors/ + cargo test --verbose + + - name: Build errorhandling + run: | + cd errorhandling/ + cargo build --verbose + + - name: Test errorhandling + run: | + cd errorhandling/ cargo test --verbose \ No newline at end of file diff --git a/README.md b/README.md index 730e00a..3eb4d21 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ A repo for learning rust - [`ownerships`](arrays) arrays. - [`enums`](enums) get to know what is enums and how to use it. - [`vectors`](vectors) vectors +- [`errorhandling`](errorhandling) learn to handle errors to become a pro :) ## Contributors: diff --git a/errorhandling/Cargo.toml b/errorhandling/Cargo.toml new file mode 100644 index 0000000..9c70955 --- /dev/null +++ b/errorhandling/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "errorhandling" +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/errorhandling/src/main.rs b/errorhandling/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/errorhandling/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} From d9e6029eb2577f931048171dc9ad9f51cb237e3a Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 7 Nov 2021 16:34:49 +0530 Subject: [PATCH 24/40] errorhandling: gitignore + README --- errorhandling/.gitignore | 10 ++++++++++ errorhandling/README.md | 2 ++ 2 files changed, 12 insertions(+) create mode 100644 errorhandling/.gitignore create mode 100644 errorhandling/README.md diff --git a/errorhandling/.gitignore b/errorhandling/.gitignore new file mode 100644 index 0000000..088ba6b --- /dev/null +++ b/errorhandling/.gitignore @@ -0,0 +1,10 @@ +# Generated by Cargo +# will have compiled files and executables +/target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk diff --git a/errorhandling/README.md b/errorhandling/README.md new file mode 100644 index 0000000..db8a87d --- /dev/null +++ b/errorhandling/README.md @@ -0,0 +1,2 @@ +## Error Handling: + From e6e09050b9cd8148bd8561ce3b19a567aad5a246 Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 7 Nov 2021 16:36:55 +0530 Subject: [PATCH 25/40] errorhandling: main errorhandling --- errorhandling/src/main.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/errorhandling/src/main.rs b/errorhandling/src/main.rs index e7a11a9..fa118dd 100644 --- a/errorhandling/src/main.rs +++ b/errorhandling/src/main.rs @@ -1,3 +1,14 @@ +use std::fs::File; + fn main() { - println!("Hello, world!"); -} + let f = File::open("test.jpg"); + + match f { + Ok(f) => { + println!("file found {:?}", f); + }, + Err(e) => { + println!("file not found \n{:?}", e); + } + } +} \ No newline at end of file From 8825887dc41e572040f833ae3e004092d6760e3f Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 7 Nov 2021 16:39:19 +0530 Subject: [PATCH 26/40] errorhandling: README --- errorhandling/README.md | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/errorhandling/README.md b/errorhandling/README.md index db8a87d..4472bc9 100644 --- a/errorhandling/README.md +++ b/errorhandling/README.md @@ -1,2 +1,43 @@ ## Error Handling: +- basic errorhandling: +```rust +fn main() { + /* we can store n as 13 it is a odd number */ + let n = 13; + + /* if n divided by 2 it is even or else it is a odd */ + if n % 2 == 0 { + println!("number is even"); + } else { + panic!("NOT_AN_EVENT"); + } +} +``` + +- advanced errorhandling: +```rust +/* we are using fs from std */ +use std::fs::File; + +fn main() { + /* open a file called test.jpg */ + let f = File::open("test.jpg"); + + match f { + /* if the file founded print file found */ + Ok(f) => { + println!("file found {:?}", f); + }, + /* else file not found */ + Err(e) => { + println!("file not found \n{:?}", e); + } + } +} +``` + + +``` +$ cargo run +``` \ No newline at end of file From 9dd2eca83b6221bf5d3ff40642d0faef21d9e32b Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 7 Nov 2021 16:42:41 +0530 Subject: [PATCH 27/40] userinput + github workflow --- .github/workflows/rust.yml | 10 ++++++++++ README.md | 2 +- userinput/.gitignore | 10 ++++++++++ userinput/Cargo.toml | 8 ++++++++ userinput/README.md | 5 +++++ userinput/src/main.rs | 3 +++ 6 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 userinput/.gitignore create mode 100644 userinput/Cargo.toml create mode 100644 userinput/README.md create mode 100644 userinput/src/main.rs diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 7161286..6ce90c1 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -132,4 +132,14 @@ jobs: - name: Test errorhandling run: | cd errorhandling/ + cargo test --verbose + + - name: Build userinput + run: | + cd userinput/ + cargo build --verbose + + - name: Test userinput + run: | + cd userinput/ cargo test --verbose \ No newline at end of file diff --git a/README.md b/README.md index 3eb4d21..fc99db5 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ A repo for learning rust - [`enums`](enums) get to know what is enums and how to use it. - [`vectors`](vectors) vectors - [`errorhandling`](errorhandling) learn to handle errors to become a pro :) - +- [`userinput`](userinput) get data from users ## Contributors: - [krishpranav](https://github.com/krishpranav) diff --git a/userinput/.gitignore b/userinput/.gitignore new file mode 100644 index 0000000..088ba6b --- /dev/null +++ b/userinput/.gitignore @@ -0,0 +1,10 @@ +# Generated by Cargo +# will have compiled files and executables +/target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk diff --git a/userinput/Cargo.toml b/userinput/Cargo.toml new file mode 100644 index 0000000..ba04edd --- /dev/null +++ b/userinput/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "userinput" +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/userinput/README.md b/userinput/README.md new file mode 100644 index 0000000..0c98151 --- /dev/null +++ b/userinput/README.md @@ -0,0 +1,5 @@ +## Userinput: + +``` +$ cargo run +``` \ No newline at end of file diff --git a/userinput/src/main.rs b/userinput/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/userinput/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} From aad42cc65cc8445fdad51b4fcd7c9a1e288bf1ec Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 7 Nov 2021 16:45:06 +0530 Subject: [PATCH 28/40] userinput --- userinput/src/main.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/userinput/src/main.rs b/userinput/src/main.rs index e7a11a9..3f35366 100644 --- a/userinput/src/main.rs +++ b/userinput/src/main.rs @@ -1,3 +1,6 @@ fn main() { - println!("Hello, world!"); -} + let mut line = String::new(); + println!("Enter your name: "); + let b1 = std::io::stdin().read_line(&mut line).unwrap(); + println!("Hello, {}", line); +} \ No newline at end of file From 50c4ac2091760c58dd056584853acfe6a9a4daa4 Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 7 Nov 2021 16:46:34 +0530 Subject: [PATCH 29/40] userinput: README --- userinput/README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/userinput/README.md b/userinput/README.md index 0c98151..fd5af2b 100644 --- a/userinput/README.md +++ b/userinput/README.md @@ -1,5 +1,24 @@ ## Userinput: +- main.rs + +- userinput +```rust +fn main() { + /* line string */ + let mut line = String::new(); + + /* enter your name */ + println!("Enter your name: "); + + /* store the user input under b1 and pass it to line */ + let b1 = std::io::stdin().read_line(&mut line).unwrap(); + + /* print the user input */ + println!("Hello: {}", line); +} +``` + ``` $ cargo run ``` \ No newline at end of file From 680e8c3d59f5cc78ae887caa15f598f9c37cea44 Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 7 Nov 2021 16:48:40 +0530 Subject: [PATCH 30/40] README --- README.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index fc99db5..4919bdb 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # learnrust A repo for learning rust +[![forthebadge](https://forthebadge.com/images/badges/made-with-rust.svg)](https://forthebadge.com) + ## Topics: - [`print`](print) basic print function. - [`datatypes`](datatypes) get to know what is datatypes. @@ -18,5 +20,13 @@ A repo for learning rust ## Contributors: - [krishpranav](https://github.com/krishpranav) -## Licensing -- learnrust is licensed under [`Apache-2.0 License`](LICENSE) \ No newline at end of file +## Licensing: +- learnrust is licensed under [`Apache-2.0 License`](LICENSE) + +## Contribution & Pull Request: +- [learnrust](https://github.com/krishpranav/learnrust) is a open source repo you'r contribution are always welcome :) + +## Social Media: +- [github](https://github.com/krishpranav) +- [twitter](https://twitter.com/krishpranav5) +- [linkedin](https://linkedin.com/in/krishpranav) \ No newline at end of file From ec524b0ce98a0c57aab878e2c34a9ba0fe04ad21 Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 7 Nov 2021 16:49:06 +0530 Subject: [PATCH 31/40] README --- README.md | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/README.md b/README.md index 4919bdb..0bdb165 100644 --- a/README.md +++ b/README.md @@ -24,9 +24,4 @@ A repo for learning rust - learnrust is licensed under [`Apache-2.0 License`](LICENSE) ## Contribution & Pull Request: -- [learnrust](https://github.com/krishpranav/learnrust) is a open source repo you'r contribution are always welcome :) - -## Social Media: -- [github](https://github.com/krishpranav) -- [twitter](https://twitter.com/krishpranav5) -- [linkedin](https://linkedin.com/in/krishpranav) \ No newline at end of file +- [learnrust](https://github.com/krishpranav/learnrust) is a open source repo you'r contribution are always welcome :) \ No newline at end of file From 49346f3d72c3140438a8c1dfcfd422f8b3f9fe96 Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 7 Nov 2021 16:51:06 +0530 Subject: [PATCH 32/40] filehandling + github workflow --- .github/workflows/rust.yml | 10 ++++++++++ filehandling/.gitignore | 10 ++++++++++ filehandling/Cargo.toml | 8 ++++++++ filehandling/README.md | 1 + filehandling/src/main.rs | 3 +++ 5 files changed, 32 insertions(+) create mode 100644 filehandling/.gitignore create mode 100644 filehandling/Cargo.toml create mode 100644 filehandling/README.md create mode 100644 filehandling/src/main.rs diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 6ce90c1..22a9b4f 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -142,4 +142,14 @@ jobs: - name: Test userinput run: | cd userinput/ + cargo test --verbose + + - name: Build filehandling + run: | + cd filehandling/ + cargo build --verbose + + - name: Test filehandling + run: | + cd filehandling/ cargo test --verbose \ No newline at end of file diff --git a/filehandling/.gitignore b/filehandling/.gitignore new file mode 100644 index 0000000..088ba6b --- /dev/null +++ b/filehandling/.gitignore @@ -0,0 +1,10 @@ +# Generated by Cargo +# will have compiled files and executables +/target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk diff --git a/filehandling/Cargo.toml b/filehandling/Cargo.toml new file mode 100644 index 0000000..13a3ad4 --- /dev/null +++ b/filehandling/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "filehandling" +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/filehandling/README.md b/filehandling/README.md new file mode 100644 index 0000000..636ce8c --- /dev/null +++ b/filehandling/README.md @@ -0,0 +1 @@ +## Filehandling \ No newline at end of file diff --git a/filehandling/src/main.rs b/filehandling/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/filehandling/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} From b84be388994454e94833d84a2c4f203bff2240d0 Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 7 Nov 2021 16:51:33 +0530 Subject: [PATCH 33/40] README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 0bdb165..a05bb15 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ A repo for learning rust - [`vectors`](vectors) vectors - [`errorhandling`](errorhandling) learn to handle errors to become a pro :) - [`userinput`](userinput) get data from users +- [`filehandling`](filehandling) filehandling functions ## Contributors: - [krishpranav](https://github.com/krishpranav) From a65d600b595e6deac8bfdd5b374acc5c625a1d05 Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 7 Nov 2021 16:53:19 +0530 Subject: [PATCH 34/40] filehandling --- filehandling/src/main.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/filehandling/src/main.rs b/filehandling/src/main.rs index e7a11a9..44796e4 100644 --- a/filehandling/src/main.rs +++ b/filehandling/src/main.rs @@ -1,3 +1,7 @@ +use std::io::Write; + fn main() { - println!("Hello, world!"); -} + let mut file = std::fs::File::create("data.txt").expect("create failed"); + file.write_all("Hello World".as_bytes()).expect("write failed"); + println!("file created: data.txt\n"); +} \ No newline at end of file From 80870d1db278bc046e813530db9ff9148795b709 Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 7 Nov 2021 16:54:33 +0530 Subject: [PATCH 35/40] filehandling: README create file and write some data + read data inside a file + delete file --- filehandling/README.md | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/filehandling/README.md b/filehandling/README.md index 636ce8c..b637ea9 100644 --- a/filehandling/README.md +++ b/filehandling/README.md @@ -1 +1,35 @@ -## Filehandling \ No newline at end of file +## Filehandling + +- main.rs + +- create a file and write something into it +```rust +use std::io::Write; + +fn main() { + let mut file = std::fs::File::create("data.txt").expect("create failed"); + file.write_all("Hello World".as_bytes()).expect("write failed"); + println!("file created: data.txt\n"); +} +``` + +- read file +```rust +use std::io::Read; + +fn main(){ + let mut file = std::fs::File::open("data.txt").unwrap(); + let mut contents = String::new(); + file.read_to_string(&mut contents).unwrap(); + print!("{}", contents); +} +``` + +- delete file +```rust +use std::fs; +fn main() { + fs::remove_file("data.txt").expect("could not remove file"); + println!("file is removed"); +} +``` \ No newline at end of file From fa549ee0097f5a15f2a139d22342da387ef804cc Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 19 Dec 2021 10:14:35 +0530 Subject: [PATCH 36/40] advfunctions --- advfunctions/.gitignore | 3 +++ advfunctions/Cargo.toml | 8 ++++++++ advfunctions/README.md | 2 ++ advfunctions/src/main.rs | 3 +++ 4 files changed, 16 insertions(+) create mode 100644 advfunctions/.gitignore create mode 100644 advfunctions/Cargo.toml create mode 100644 advfunctions/README.md create mode 100644 advfunctions/src/main.rs diff --git a/advfunctions/.gitignore b/advfunctions/.gitignore new file mode 100644 index 0000000..4b1d491 --- /dev/null +++ b/advfunctions/.gitignore @@ -0,0 +1,3 @@ +/target + +.DS_Store \ No newline at end of file diff --git a/advfunctions/Cargo.toml b/advfunctions/Cargo.toml new file mode 100644 index 0000000..cbc6e22 --- /dev/null +++ b/advfunctions/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "advfunctions" +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/advfunctions/README.md b/advfunctions/README.md new file mode 100644 index 0000000..98e2957 --- /dev/null +++ b/advfunctions/README.md @@ -0,0 +1,2 @@ +## Adv functions +- calling function from other files \ No newline at end of file diff --git a/advfunctions/src/main.rs b/advfunctions/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/advfunctions/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} From 43307c45f2ce47f66cc005d27e0b3f28213815bd Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 19 Dec 2021 11:13:27 +0530 Subject: [PATCH 37/40] advfunctions: hello --- advfunctions/src/hello.rs | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 advfunctions/src/hello.rs diff --git a/advfunctions/src/hello.rs b/advfunctions/src/hello.rs new file mode 100644 index 0000000..4afe684 --- /dev/null +++ b/advfunctions/src/hello.rs @@ -0,0 +1,3 @@ +pub fn helloworld() { + println!("Hello, World!"); +} \ No newline at end of file From 973dc2fcb56526e2026f8a9c4b81732e7b65cf11 Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 19 Dec 2021 11:14:31 +0530 Subject: [PATCH 38/40] src: main call function from another file --- advfunctions/src/main.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/advfunctions/src/main.rs b/advfunctions/src/main.rs index e7a11a9..67222c9 100644 --- a/advfunctions/src/main.rs +++ b/advfunctions/src/main.rs @@ -1,3 +1,5 @@ +mod hello; + fn main() { - println!("Hello, world!"); -} + hello::helloworld(); +} \ No newline at end of file From 319ca69d3dc80ce064295059f79491aa02fa4e8c Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 19 Dec 2021 11:15:24 +0530 Subject: [PATCH 39/40] README --- advfunctions/README.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/advfunctions/README.md b/advfunctions/README.md index 98e2957..0090193 100644 --- a/advfunctions/README.md +++ b/advfunctions/README.md @@ -1,2 +1,23 @@ ## Adv functions -- calling function from other files \ No newline at end of file +- calling function from other files + + +- hello.rs: +```rust +pub fn helloworld() { + println!("Hello, World!"); +} +``` + +- main.rs: +```rust +mod hello; + +fn main() { + hello::helloworld(); +} +``` + +``` +$ cargo run +``` \ No newline at end of file From 861ac07422384c52b4373d210e2654836deb75c1 Mon Sep 17 00:00:00 2001 From: krishpranav Date: Sun, 19 Dec 2021 11:16:17 +0530 Subject: [PATCH 40/40] README --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a05bb15..9ebfc4f 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,8 @@ A repo for learning rust - [`errorhandling`](errorhandling) learn to handle errors to become a pro :) - [`userinput`](userinput) get data from users - [`filehandling`](filehandling) filehandling functions +- [`advfunctions`](advfunctions) call functions from another file :) + ## Contributors: - [krishpranav](https://github.com/krishpranav) @@ -25,4 +27,4 @@ A repo for learning rust - learnrust is licensed under [`Apache-2.0 License`](LICENSE) ## Contribution & Pull Request: -- [learnrust](https://github.com/krishpranav/learnrust) is a open source repo you'r contribution are always welcome :) \ No newline at end of file +- [learnrust](https://github.com/krishpranav/learnrust) is a open source repo you'r contribution are always welcome :)