diff --git a/.DS_Store b/.DS_Store index 96d12d2..9fe6db9 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/chapter1/.DS_Store b/chapter1/.DS_Store new file mode 100644 index 0000000..b0a6ddb Binary files /dev/null and b/chapter1/.DS_Store differ diff --git a/chapter1/bench-test/.DS_Store b/chapter1/bench-test/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/chapter1/bench-test/.DS_Store differ diff --git a/chapter1/bench-test/Cargo.lock b/chapter1/bench-test/Cargo.lock new file mode 100644 index 0000000..cb164a9 --- /dev/null +++ b/chapter1/bench-test/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "bench-test" +version = "0.1.0" diff --git a/chapter1/bench-test/Cargo.toml b/chapter1/bench-test/Cargo.toml new file mode 100644 index 0000000..62dc867 --- /dev/null +++ b/chapter1/bench-test/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "bench-test" +version = "0.1.0" +authors = ["peshwar9 "] +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 0000000..5008ddf Binary files /dev/null and b/chapter1/dep-example/.DS_Store differ diff --git a/chapter1/dep-example/Cargo.lock b/chapter1/dep-example/Cargo.lock new file mode 100644 index 0000000..b6bd847 --- /dev/null +++ b/chapter1/dep-example/Cargo.lock @@ -0,0 +1,82 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "autocfg" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d" + +[[package]] +name = "chrono" +version = "0.4.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80094f509cf8b5ae86a4966a39b3ff66cd7e2a3e594accec3743ff3fabeab5b2" +dependencies = [ + "num-integer", + "num-traits", + "time", +] + +[[package]] +name = "dep-example" +version = "0.1.0" +dependencies = [ + "chrono", +] + +[[package]] +name = "libc" +version = "0.2.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99e85c08494b21a9054e7fe1374a732aeadaff3980b6990b94bfd3a70f690005" + +[[package]] +name = "num-integer" +version = "0.1.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f6ea62e9d81a77cd3ee9a2a5b9b609447857f3d358704331e4ef39eb247fcba" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c62be47e61d1842b9170f0fdeec8eba98e60e90e5446449a0545e5152acd7096" +dependencies = [ + "autocfg", +] + +[[package]] +name = "time" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438" +dependencies = [ + "libc", + "winapi", +] + +[[package]] +name = "winapi" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/chapter1/dep-example/Cargo.toml b/chapter1/dep-example/Cargo.toml new file mode 100644 index 0000000..1222dcc --- /dev/null +++ b/chapter1/dep-example/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "dep-example" +version = "0.1.0" +authors = ["peshwar9 "] +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 0000000..5008ddf Binary files /dev/null and b/chapter1/feature-test/.DS_Store differ diff --git a/chapter1/feature-test/Cargo.lock b/chapter1/feature-test/Cargo.lock new file mode 100644 index 0000000..2249d11 --- /dev/null +++ b/chapter1/feature-test/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "feature-test" +version = "0.1.0" diff --git a/chapter1/feature-test/Cargo.toml b/chapter1/feature-test/Cargo.toml new file mode 100644 index 0000000..35c27ca --- /dev/null +++ b/chapter1/feature-test/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "feature-test" +version = "0.1.0" +authors = ["peshwar9 "] +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 0000000..5008ddf Binary files /dev/null and b/chapter1/first-program/.DS_Store differ diff --git a/chapter1/first-program/Cargo.lock b/chapter1/first-program/Cargo.lock new file mode 100644 index 0000000..9565532 --- /dev/null +++ b/chapter1/first-program/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "first-program" +version = "0.1.0" diff --git a/chapter1/first-program/Cargo.toml b/chapter1/first-program/Cargo.toml new file mode 100644 index 0000000..cdefca9 --- /dev/null +++ b/chapter1/first-program/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "first-program" +version = "0.1.0" +authors = ["peshwar9 "] +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 0000000..5008ddf Binary files /dev/null and b/chapter1/integ-test-example/.DS_Store differ diff --git a/chapter1/integ-test-example/Cargo.lock b/chapter1/integ-test-example/Cargo.lock new file mode 100644 index 0000000..b6bc3a7 --- /dev/null +++ b/chapter1/integ-test-example/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "integ-test-example" +version = "0.1.0" diff --git a/chapter1/integ-test-example/Cargo.toml b/chapter1/integ-test-example/Cargo.toml new file mode 100644 index 0000000..add807f --- /dev/null +++ b/chapter1/integ-test-example/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "integ-test-example" +version = "0.1.0" +authors = ["peshwar9 "] +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 0000000..5008ddf Binary files /dev/null and b/chapter1/my-first-lib/.DS_Store differ diff --git a/chapter1/my-first-lib/Cargo.lock b/chapter1/my-first-lib/Cargo.lock new file mode 100644 index 0000000..18a3208 --- /dev/null +++ b/chapter1/my-first-lib/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "my-first-lib" +version = "0.1.0" diff --git a/chapter1/my-first-lib/Cargo.toml b/chapter1/my-first-lib/Cargo.toml new file mode 100644 index 0000000..996a460 --- /dev/null +++ b/chapter1/my-first-lib/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "my-first-lib" +version = "0.1.0" +authors = ["peshwar9 "] +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 0000000..5008ddf Binary files /dev/null and b/chapter1/test-example/.DS_Store differ diff --git a/chapter1/test-example/Cargo.lock b/chapter1/test-example/Cargo.lock new file mode 100644 index 0000000..69d9894 --- /dev/null +++ b/chapter1/test-example/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "test-example" +version = "0.1.0" diff --git a/chapter1/test-example/Cargo.toml b/chapter1/test-example/Cargo.toml new file mode 100644 index 0000000..c8451ea --- /dev/null +++ b/chapter1/test-example/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "test-example" +version = "0.1.0" +authors = ["peshwar9 "] +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 0000000..5008ddf Binary files /dev/null and b/chapter8/sec1/.DS_Store differ diff --git a/chapter8/sec1/Cargo.lock b/chapter8/sec1/Cargo.lock new file mode 100644 index 0000000..130d0ed --- /dev/null +++ b/chapter8/sec1/Cargo.lock @@ -0,0 +1,40 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "arc-swap" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d25d88fd6b8041580a654f9d0c581a047baee2b3efee13275f2fc392fc75034" + +[[package]] +name = "libc" +version = "0.2.77" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2f96b10ec2560088a8e76961b00d47107b3a625fecb76dedb29ee7ccbf98235" + +[[package]] +name = "sec1" +version = "0.1.0" +dependencies = [ + "signal-hook", +] + +[[package]] +name = "signal-hook" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "604508c1418b99dfe1925ca9224829bb2a8a9a04dda655cc01fcad46f4ab05ed" +dependencies = [ + "libc", + "signal-hook-registry", +] + +[[package]] +name = "signal-hook-registry" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3e12110bc539e657a646068aaf5eb5b63af9d0c1f7b29c97113fad80e15f035" +dependencies = [ + "arc-swap", + "libc", +] diff --git a/chapter8/sec1/Cargo.toml b/chapter8/sec1/Cargo.toml new file mode 100644 index 0000000..faf730e --- /dev/null +++ b/chapter8/sec1/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "sec1" +version = "0.1.0" +authors = ["peshwar9 "] +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(); + } +}