diff --git a/ch14/add/Cargo.lock b/ch14/add/Cargo.lock new file mode 100644 index 0000000..96a804b --- /dev/null +++ b/ch14/add/Cargo.lock @@ -0,0 +1,140 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "add_one" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "adder" +version = "0.1.0" +dependencies = [ + "add_one", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "libc" +version = "0.2.171" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c19937216e9d3aa9956d9bb8dfc0b0c8beb6058fc4f7a4dc4d850edf86a237d6" + +[[package]] +name = "ppv-lite86" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "proc-macro2" +version = "1.0.94" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a31971752e70b8b2686d7e46ec17fb38dad4051d94024c88df49b667caea9c84" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "syn" +version = "2.0.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b09a44accad81e1ba1cd74a32461ba89dee89095ba17b32f5d03683b1b1fc2a0" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "zerocopy" +version = "0.8.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2586fea28e186957ef732a5f8b3be2da217d65c5969d4b1e17f973ebbe876879" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a996a8f63c5c4448cd959ac1bab0aaa3306ccfd060472f85943ee0750f0169be" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/ch14/add/Cargo.toml b/ch14/add/Cargo.toml new file mode 100644 index 0000000..3da84b9 --- /dev/null +++ b/ch14/add/Cargo.toml @@ -0,0 +1,6 @@ +[workspace] + +members = [ + "adder", + "add_one", +] diff --git a/ch14/add/add_one/Cargo.toml b/ch14/add/add_one/Cargo.toml new file mode 100644 index 0000000..bc758bd --- /dev/null +++ b/ch14/add/add_one/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "add_one" +version = "0.1.0" +edition = "2021" + +[dependencies] +rand = "0.8.5" diff --git a/ch14/add/add_one/src/lib.rs b/ch14/add/add_one/src/lib.rs new file mode 100644 index 0000000..9524fe1 --- /dev/null +++ b/ch14/add/add_one/src/lib.rs @@ -0,0 +1,21 @@ +use rand; + +pub fn add(left: u64, right: u64) -> u64 { + left + right +} + +pub fn add_one(x: i32) -> i32 { + x + 1 +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() { + // let result = add(2, 2); + // assert_eq!(result, 4); + assert_eq!(3, add_one(2)); + } +} diff --git a/ch14/add/adder/Cargo.toml b/ch14/add/adder/Cargo.toml new file mode 100644 index 0000000..55c0203 --- /dev/null +++ b/ch14/add/adder/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "adder" +version = "0.1.0" +edition = "2021" + +[dependencies] +add_one = { path = "../add_one" } diff --git a/ch14/add/adder/src/main.rs b/ch14/add/adder/src/main.rs new file mode 100644 index 0000000..1316294 --- /dev/null +++ b/ch14/add/adder/src/main.rs @@ -0,0 +1,6 @@ +use add_one; + +fn main() { + let num = 10; + println!("Hello, world! {num} plus one is {}!", add_one::add_one(num)); +} diff --git a/ch14/publishing-to-crates-io/Cargo.lock b/ch14/publishing-to-crates-io/Cargo.lock new file mode 100644 index 0000000..930ccc1 --- /dev/null +++ b/ch14/publishing-to-crates-io/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "publishing-to-crates-io" +version = "0.1.0" diff --git a/ch14/publishing-to-crates-io/Cargo.toml b/ch14/publishing-to-crates-io/Cargo.toml new file mode 100644 index 0000000..f5060f0 --- /dev/null +++ b/ch14/publishing-to-crates-io/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "publishing-to-crates-io" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/ch14/publishing-to-crates-io/src/lib.rs b/ch14/publishing-to-crates-io/src/lib.rs new file mode 100644 index 0000000..750ce69 --- /dev/null +++ b/ch14/publishing-to-crates-io/src/lib.rs @@ -0,0 +1,48 @@ +//! # Art +//! +//! A library for modeling artistic concepts + +// These are re-exports +pub use self::kinds::PrimaryColor; +pub use self::kinds::SecondaryColor; +pub use self::utils::mix; + +pub mod kinds { + pub enum PrimaryColor { + Red, + Yellow, + Blue, + } + + pub enum SecondaryColor { + Orange, + Green, + Purple, + } +} + +pub mod utils { + use crate::kinds::*; + + /// Combines two primary colors in equal amounts to create + /// a secondary color + pub fn mix(c1: PrimaryColor, c2: PrimaryColor) -> SecondaryColor { + SecondaryColor::Orange + // unimplemented!() + } +} + +pub fn add(left: u64, right: u64) -> u64 { + left + right +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() { + let result = add(2, 2); + assert_eq!(result, 4); + } +} diff --git a/ch14/publishing-to-crates-io/src/main.rs b/ch14/publishing-to-crates-io/src/main.rs new file mode 100644 index 0000000..1f5cd4d --- /dev/null +++ b/ch14/publishing-to-crates-io/src/main.rs @@ -0,0 +1,8 @@ +use publishing_to_crates_io::kinds::PrimaryColor; +use publishing_to_crates_io::utils::mix; + +fn main() { + let red = PrimaryColor::Red; + let yellow = PrimaryColor::Yellow; + mix(red, yellow); +} diff --git a/ch14/release-profiles/Cargo.lock b/ch14/release-profiles/Cargo.lock new file mode 100644 index 0000000..f6ce198 --- /dev/null +++ b/ch14/release-profiles/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "release-profiles" +version = "0.1.0" diff --git a/ch14/release-profiles/Cargo.toml b/ch14/release-profiles/Cargo.toml new file mode 100644 index 0000000..eb1c461 --- /dev/null +++ b/ch14/release-profiles/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "release-profiles" +version = "0.1.0" +edition = "2021" + +[dependencies] + +# Cargo has default settings for each of the profiles +# for building in the `[profiles.*]` sections. +# opt-level controls the amount of optimizations appied +# at compile time. +# 1 is not the default value +[profile.dev] +opt-level = 1 + +[profile.release] +opt-level = 3 diff --git a/ch14/release-profiles/src/lib.rs b/ch14/release-profiles/src/lib.rs new file mode 100644 index 0000000..8ca0c6c --- /dev/null +++ b/ch14/release-profiles/src/lib.rs @@ -0,0 +1,30 @@ +//! # Release Profiles +//! +//! `release_profiles` is a collection of utilities to +//! learn Rust. +//! +//! The doubles slashes and then ! adds documentation to +//! the item that contains the comments rather than to the +//! items following the comments. Typically used in crate root +//! file. +//! +//! Documentation comments have three slashes which +//! generate HTML. + +/// Adds one to the given number. +/// This is literally markdown +/// +/// # Examples +/// +/// ``` +/// let arg = 5; +/// let answer = release_profiles::add_one(arg); +/// +/// assert_eq!(6, answer); +/// ``` +pub fn add_one(x: i32) -> i32 { + x + 1 +} + +// Like the `# Examples` section above, +// may also use `Panics`, `Errors`, or `Safety` diff --git a/ch14/release-profiles/src/main.rs b/ch14/release-profiles/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/ch14/release-profiles/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +}