Skip to content

Commit 34bc5cc

Browse files
committed
Add cargo workspace example
1 parent f8d8ce0 commit 34bc5cc

File tree

7 files changed

+144
-0
lines changed

7 files changed

+144
-0
lines changed

README.org

+1
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,5 @@ The official [[https://github.com/tokio-rs/tokio][Tokio]] (asynchronous runtime
6161
The official rust book - [[https://github.com/rust-lang/book][The Rust Programming Language]].
6262

6363
- [[file:the-book/minigrep/src/main.rs][chapter 12: building a command line program]]
64+
- [[file:the-book/add/adder/src/main.rs][chapter 14.3: cargo workspaces]]
6465
- [[file:the-book/hello/src/bin/main.rs][chapter 20: building a multithreaded web server]]

the-book/add/Cargo.lock

+83
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

the-book/add/Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[workspace]
2+
3+
members = [
4+
"adder",
5+
"add_one",
6+
]

the-book/add/add_one/Cargo.toml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "add_one"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
rand = "0.8.5"

the-book/add/add_one/src/lib.rs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// use rand;
2+
3+
pub fn add(left: usize, right: usize) -> usize {
4+
left + right
5+
}
6+
7+
pub fn add_one(x: i32) -> i32 {
8+
x + 1
9+
}
10+
11+
#[cfg(test)]
12+
mod tests {
13+
use super::*;
14+
15+
#[test]
16+
fn it_works() {
17+
let result = add(2, 2);
18+
assert_eq!(result, 4);
19+
}
20+
21+
#[test]
22+
fn test_add_one() {
23+
assert_eq!(add_one(5), 6);
24+
}
25+
}

the-book/add/adder/Cargo.toml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "adder"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
add_one = { path = "../add_one" }
10+
rand = "0.8.5"

the-book/add/adder/src/main.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use add_one;
2+
use rand::prelude::*;
3+
4+
fn main() {
5+
let num = 10;
6+
println!("Hello, world! {num} plus one is {}!", add_one::add_one(num));
7+
let mut rng = rand::thread_rng();
8+
let y: f64 = rng.gen();
9+
println!("random: {y}");
10+
}

0 commit comments

Comments
 (0)