From 13c7dc408102d070fb96ac81c98fb0789708c2a2 Mon Sep 17 00:00:00 2001 From: peshwar9 Date: Thu, 1 Oct 2020 17:47:24 +0530 Subject: [PATCH] Added chapters 1 and 8 --- .DS_Store | Bin 6148 -> 6148 bytes chapter1/.DS_Store | Bin 0 -> 8196 bytes chapter1/bench-test/.DS_Store | Bin 0 -> 6148 bytes chapter1/bench-test/Cargo.lock | 5 ++ chapter1/bench-test/Cargo.toml | 9 ++ chapter1/bench-test/src/lib.rs | 27 ++++++ chapter1/bench-test/src/main.rs | 3 + chapter1/dep-example/.DS_Store | Bin 0 -> 6148 bytes chapter1/dep-example/Cargo.lock | 82 ++++++++++++++++++ chapter1/dep-example/Cargo.toml | 10 +++ chapter1/dep-example/src/main.rs | 5 ++ chapter1/feature-test/.DS_Store | Bin 0 -> 6148 bytes chapter1/feature-test/Cargo.lock | 5 ++ chapter1/feature-test/Cargo.toml | 16 ++++ chapter1/feature-test/src/lib.rs | 19 ++++ chapter1/feature-test/src/main.rs | 5 ++ chapter1/first-program/.DS_Store | Bin 0 -> 6148 bytes chapter1/first-program/Cargo.lock | 5 ++ chapter1/first-program/Cargo.toml | 17 ++++ chapter1/first-program/src/main.rs | 3 + chapter1/first-program/src/second.rs | 3 + chapter1/integ-test-example/.DS_Store | Bin 0 -> 6148 bytes chapter1/integ-test-example/Cargo.lock | 5 ++ chapter1/integ-test-example/Cargo.toml | 9 ++ chapter1/integ-test-example/doc/itest.html | 42 +++++++++ chapter1/integ-test-example/doc/itest.md | 24 +++++ chapter1/integ-test-example/src/lib.rs | 17 ++++ chapter1/integ-test-example/src/main.rs | 3 + .../tests/integration_test1.rs | 21 +++++ chapter1/my-first-lib/.DS_Store | Bin 0 -> 6148 bytes chapter1/my-first-lib/Cargo.lock | 5 ++ chapter1/my-first-lib/Cargo.toml | 17 ++++ chapter1/my-first-lib/src/lib.rs | 12 +++ chapter1/my-first-lib/src/mymain.rs | 6 ++ chapter1/test-example/.DS_Store | Bin 0 -> 6148 bytes chapter1/test-example/Cargo.lock | 5 ++ chapter1/test-example/Cargo.toml | 9 ++ chapter1/test-example/src/main.rs | 19 ++++ .../test-example/tests/integration_test1.rs | 7 ++ chapter8/sec1/.DS_Store | Bin 0 -> 6148 bytes chapter8/sec1/Cargo.lock | 40 +++++++++ chapter8/sec1/Cargo.toml | 10 +++ chapter8/sec1/a.txt | 1 + chapter8/sec1/b.txt | 6 ++ chapter8/sec1/src/main.rs | 37 ++++++++ 45 files changed, 509 insertions(+) create mode 100644 chapter1/.DS_Store create mode 100644 chapter1/bench-test/.DS_Store create mode 100644 chapter1/bench-test/Cargo.lock create mode 100644 chapter1/bench-test/Cargo.toml create mode 100644 chapter1/bench-test/src/lib.rs create mode 100644 chapter1/bench-test/src/main.rs create mode 100644 chapter1/dep-example/.DS_Store create mode 100644 chapter1/dep-example/Cargo.lock create mode 100644 chapter1/dep-example/Cargo.toml create mode 100644 chapter1/dep-example/src/main.rs create mode 100644 chapter1/feature-test/.DS_Store create mode 100644 chapter1/feature-test/Cargo.lock create mode 100644 chapter1/feature-test/Cargo.toml create mode 100644 chapter1/feature-test/src/lib.rs create mode 100644 chapter1/feature-test/src/main.rs create mode 100644 chapter1/first-program/.DS_Store create mode 100644 chapter1/first-program/Cargo.lock create mode 100644 chapter1/first-program/Cargo.toml create mode 100644 chapter1/first-program/src/main.rs create mode 100644 chapter1/first-program/src/second.rs create mode 100644 chapter1/integ-test-example/.DS_Store create mode 100644 chapter1/integ-test-example/Cargo.lock create mode 100644 chapter1/integ-test-example/Cargo.toml create mode 100644 chapter1/integ-test-example/doc/itest.html create mode 100644 chapter1/integ-test-example/doc/itest.md create mode 100644 chapter1/integ-test-example/src/lib.rs create mode 100644 chapter1/integ-test-example/src/main.rs create mode 100644 chapter1/integ-test-example/tests/integration_test1.rs create mode 100644 chapter1/my-first-lib/.DS_Store create mode 100644 chapter1/my-first-lib/Cargo.lock create mode 100644 chapter1/my-first-lib/Cargo.toml create mode 100644 chapter1/my-first-lib/src/lib.rs create mode 100644 chapter1/my-first-lib/src/mymain.rs create mode 100644 chapter1/test-example/.DS_Store create mode 100644 chapter1/test-example/Cargo.lock create mode 100644 chapter1/test-example/Cargo.toml create mode 100644 chapter1/test-example/src/main.rs create mode 100644 chapter1/test-example/tests/integration_test1.rs create mode 100644 chapter8/sec1/.DS_Store create mode 100644 chapter8/sec1/Cargo.lock create mode 100644 chapter8/sec1/Cargo.toml create mode 100644 chapter8/sec1/a.txt create mode 100644 chapter8/sec1/b.txt create mode 100644 chapter8/sec1/src/main.rs diff --git a/.DS_Store b/.DS_Store index 96d12d23ee7146296613680c7b9444e857139f73..9fe6db94923443019e76d6c8776960e717e11030 100644 GIT binary patch delta 288 zcmZoMXfc=|#>B!ku~2NHo}!=t0|Nsi0|SsMVMt{tVldp8u$pmk05g*)NQQ$UnIVHA zk)Z%4J6VBAIoL2a-^C>UCqyHxza>S-OH$R0abp;udcQGk$mS<96oP3%| zfYAbEO`rv`HQc)luHmo-s19KbK6RUQm{&7yX6NAN0Q!HkBlCCW$^0UY96;|g0bS3q JIYML&GXRdMMw9>m delta 89 zcmZoMXfc=|#>B)qu~2NHo}wTJ0|Nsi1A_nqLkUAFLlJ|?#)Q?36ZP08e`Vp{yn{ua padSS~491NO2F#n;Iruq%8aE4ad}p4_FX9N&3ewB6IYML&GXR{%6bt|W diff --git a/chapter1/.DS_Store b/chapter1/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..b0a6ddb7cef824b3e9efcfdd469804d07e7a47c1 GIT binary patch literal 8196 zcmeHMziZn-6n+{uMc3MJ8<^9SWjrxBd?u zGPR`;Lg?TY3W59~Sp$Kt?f333_6hqGb!v&JZeSkqJw%7!XiVki@i`GD!b z&|+rLgp)Gir0kWItx%M{I^qINClwl$Q5+BlCLQ3~eSuo2*$=4g*6;mzeYc$^0~1o; z^zo83idy}&h572q^U9~+AAUUbb33KEgPwc+A(o^?>OkiJdkkGVrY0pg?o&6f^x-c* z)7ARu{O1>|)w6Drj@91H&g=cmdY5PqN}JdVIif=xjrKH1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0"] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/chapter1/bench-test/src/lib.rs b/chapter1/bench-test/src/lib.rs new file mode 100644 index 0000000..9f15317 --- /dev/null +++ b/chapter1/bench-test/src/lib.rs @@ -0,0 +1,27 @@ +#![feature(test)] + +extern crate test; + +pub fn get_fact(n: u64) -> u64 { + if n < 2 { + 1 + } else { + n * get_fact(n - 1) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use test::Bencher; + + #[test] + fn it_works() { + assert_eq!(120, get_fact(5)); + } + + #[bench] + fn bench_get_fact(b: &mut Bencher) { + b.iter(|| get_fact(1234571)); + } +} diff --git a/chapter1/bench-test/src/main.rs b/chapter1/bench-test/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/chapter1/bench-test/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/chapter1/dep-example/.DS_Store b/chapter1/dep-example/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0"] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +chrono = "*" diff --git a/chapter1/dep-example/src/main.rs b/chapter1/dep-example/src/main.rs new file mode 100644 index 0000000..6333a45 --- /dev/null +++ b/chapter1/dep-example/src/main.rs @@ -0,0 +1,5 @@ +use chrono as time; + +fn main() { + println!("Hello, time now is {:?}", time::Utc::now()); +} diff --git a/chapter1/feature-test/.DS_Store b/chapter1/feature-test/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0"] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[[bin]] +name = "featuretest" +path = "src/main.rs" + +[features] +feature1 = [] + +[dependencies] diff --git a/chapter1/feature-test/src/lib.rs b/chapter1/feature-test/src/lib.rs new file mode 100644 index 0000000..814b4bf --- /dev/null +++ b/chapter1/feature-test/src/lib.rs @@ -0,0 +1,19 @@ +#[cfg(feature = "feature1")] +pub use self::with_feature_config::execute; + +#[cfg(not(feature = "feature1"))] +pub use self::without_feature_config::execute; + +#[cfg(feature = "feature1")] +mod with_feature_config { + pub fn execute() { + println!("Executing feature 1"); + } +} + +#[cfg(not(feature = "feature1"))] +mod without_feature_config { + pub fn execute() { + println!("Not executing feature 1") + } +} diff --git a/chapter1/feature-test/src/main.rs b/chapter1/feature-test/src/main.rs new file mode 100644 index 0000000..ba7935b --- /dev/null +++ b/chapter1/feature-test/src/main.rs @@ -0,0 +1,5 @@ +use feature_test::execute; + +fn main() { + execute(); +} diff --git a/chapter1/first-program/.DS_Store b/chapter1/first-program/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0"] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[[bin]] +name = "new-first-program" +path = "src/main.rs" + +[[bin]] +name = "new-second-program" +path = "src/second.rs" + +[dependencies] diff --git a/chapter1/first-program/src/main.rs b/chapter1/first-program/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/chapter1/first-program/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/chapter1/first-program/src/second.rs b/chapter1/first-program/src/second.rs new file mode 100644 index 0000000..c9ea8bc --- /dev/null +++ b/chapter1/first-program/src/second.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello for the second time!") +} diff --git a/chapter1/integ-test-example/.DS_Store b/chapter1/integ-test-example/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0"] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/chapter1/integ-test-example/doc/itest.html b/chapter1/integ-test-example/doc/itest.html new file mode 100644 index 0000000..985c315 --- /dev/null +++ b/chapter1/integ-test-example/doc/itest.html @@ -0,0 +1,42 @@ + + + + + + + Docs for integ-test-example crate + + + + + + + + +

Docs for integ-test-example crate

+

This is a project to test rustdoc.

+

Here is a link!

+

0.1 Function signature

+

pub fn get_process_id() -> u32 {}

+

This function returns the process id of the current running executable

+

0.2 Example

+
+
+use integ_test_example;
+
+fn get_id() -> i32 {
+ let my_pid = get_process_id();
+ println!("Process id for current process is: {}", my_pid);    
+}
+
+ + + + \ No newline at end of file diff --git a/chapter1/integ-test-example/doc/itest.md b/chapter1/integ-test-example/doc/itest.md new file mode 100644 index 0000000..8634871 --- /dev/null +++ b/chapter1/integ-test-example/doc/itest.md @@ -0,0 +1,24 @@ +# Docs for integ-test-example crate + +This is a project to test `rustdoc`. + +[Here is a link!](https://www.rust-lang.org) + +## Function signature + +pub fn get_process_id() -> u32 {} + +This function returns the process id of the current running executable + +## Example + +```rust + +use integ_test_example; + +fn get_id() { + let my_pid = get_process_id(); + println!("Process id for current process is: {}", my_pid); +} + +``` diff --git a/chapter1/integ-test-example/src/lib.rs b/chapter1/integ-test-example/src/lib.rs new file mode 100644 index 0000000..c151b12 --- /dev/null +++ b/chapter1/integ-test-example/src/lib.rs @@ -0,0 +1,17 @@ + +//! Integration-test-example crate +//! +//! This is a library that contains functions related to dealing with processes +//! , and makes these tasks more convenient. + +use std::process; +/// This function gets the process id of the current executable. It returns a non-zero number +/// ``` +/// fn get_id() { +/// let x = integ_test_example::get_process_id(); +/// println!("{}",x); +/// } +/// ``` +pub fn get_process_id() -> u32 { + process::id() +} diff --git a/chapter1/integ-test-example/src/main.rs b/chapter1/integ-test-example/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/chapter1/integ-test-example/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/chapter1/integ-test-example/tests/integration_test1.rs b/chapter1/integ-test-example/tests/integration_test1.rs new file mode 100644 index 0000000..ecbc040 --- /dev/null +++ b/chapter1/integ-test-example/tests/integration_test1.rs @@ -0,0 +1,21 @@ +use integ_test_example; +#[test] +fn files_test1() { + assert_ne!(integ_test_example::get_process_id(), 0, "Error in code"); +} +/* Hello this is a multiline + * comment + */ + +// Hello this is a single line comment + +#[test] +fn files_test2() { + assert_eq!(1 + 1, 2); +} + +#[test] +#[ignore] +fn process_test1() { + assert!(true); +} diff --git a/chapter1/my-first-lib/.DS_Store b/chapter1/my-first-lib/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0"] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[[bin]] +name = "mymain" +path = "src/mymain.rs" + +[lib] +crate-type = ["dylib"] + + +[dependencies] diff --git a/chapter1/my-first-lib/src/lib.rs b/chapter1/my-first-lib/src/lib.rs new file mode 100644 index 0000000..08f1d53 --- /dev/null +++ b/chapter1/my-first-lib/src/lib.rs @@ -0,0 +1,12 @@ +pub fn hello_from_lib(message: & str) { + println!("Printing Hello {} from library",message); +} + + +#[cfg(test)] +mod tests { + #[test] + fn it_works() { + assert_eq!(2 + 2, 4); + } +} diff --git a/chapter1/my-first-lib/src/mymain.rs b/chapter1/my-first-lib/src/mymain.rs new file mode 100644 index 0000000..6d9b2e8 --- /dev/null +++ b/chapter1/my-first-lib/src/mymain.rs @@ -0,0 +1,6 @@ +use my_first_lib::hello_from_lib; + +fn main() { + println!("Going to call library function"); + hello_from_lib("Rust system programmer"); +} diff --git a/chapter1/test-example/.DS_Store b/chapter1/test-example/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0"] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/chapter1/test-example/src/main.rs b/chapter1/test-example/src/main.rs new file mode 100644 index 0000000..3008424 --- /dev/null +++ b/chapter1/test-example/src/main.rs @@ -0,0 +1,19 @@ +use std::process; + +fn main() { + println!("{}", get_process_id()); +} + +fn get_process_id() -> u32 { + process::id() +} + +#[cfg(test)] +mod tests { + use super::get_process_id; + + #[test] + fn test_if_process_id_is_returned() { + assert_ne!(get_process_id(), 0, "There is error in code"); + } +} diff --git a/chapter1/test-example/tests/integration_test1.rs b/chapter1/test-example/tests/integration_test1.rs new file mode 100644 index 0000000..d243261 --- /dev/null +++ b/chapter1/test-example/tests/integration_test1.rs @@ -0,0 +1,7 @@ +use my-process-lib; + + #[test] + fn test_if_process_id_is_returned() { + assert_eq!(get_process_id(), 0, "There is error in code"); + println!("Hello from integration test") + } diff --git a/chapter8/sec1/.DS_Store b/chapter8/sec1/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0"] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +signal-hook = "0.1.16" diff --git a/chapter8/sec1/a.txt b/chapter8/sec1/a.txt new file mode 100644 index 0000000..802992c --- /dev/null +++ b/chapter8/sec1/a.txt @@ -0,0 +1 @@ +Hello world diff --git a/chapter8/sec1/b.txt b/chapter8/sec1/b.txt new file mode 100644 index 0000000..86e68f1 --- /dev/null +++ b/chapter8/sec1/b.txt @@ -0,0 +1,6 @@ +Cargo.lock +Cargo.toml +a.txt +b.txt +src +target diff --git a/chapter8/sec1/src/main.rs b/chapter8/sec1/src/main.rs new file mode 100644 index 0000000..18c9de0 --- /dev/null +++ b/chapter8/sec1/src/main.rs @@ -0,0 +1,37 @@ +use std::io::Write; +use std::io::{stdin, stdout}; +use std::process::Command; + +fn main() { + loop { + print!("$"); + stdout().flush().unwrap(); + let mut user_input = String::new(); + stdin() + .read_line(&mut user_input) + .expect("Unable to read user input"); + let command_to_execute = user_input.trim(); + let command_args: Vec<&str> = command_to_execute.split_whitespace().collect(); + let mut child = match command_args[0] { + "show" => match command_args[1] { + "files" => Command::new("ls") + .args(&command_args[2..]) + .spawn() + .expect("Unable to execute command"), + "process" => Command::new("ps") + .args(&command_args[2..]) + .spawn() + .expect("Unable to execute command"), + _ => Command::new("pwd") + .args(&command_args[2..]) + .spawn() + .expect("Unable to execute command"), + }, + _ => Command::new(command_args[0]) + .args(&command_args[1..]) + .spawn() + .expect("Unable to execute command"), + }; + child.wait().unwrap(); + } +}