Skip to content

Commit d5436a2

Browse files
committed
added modules example
1 parent a43b5b1 commit d5436a2

File tree

6 files changed

+40
-0
lines changed

6 files changed

+40
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ in a controlled repository now.
1515
* `rc`: examples with `RefCell` and `Rc`
1616
* `readline`: example of readline with error handling
1717
* `error`: example of creating a custom error
18+
* `modules`: example of Rust modules

modules/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[package]
2+
name = "modules"
3+
version = "0.1.0"
4+
authors = ["Bart Massey <[email protected]>"]
5+
edition = "2018"

modules/src/fancymod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub fn fancy_hello() {
2+
println!("HELOOO world!!?!");
3+
}

modules/src/main.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
mod fancymod;
2+
mod reallyfancymod;
3+
4+
use reallyfancymod::subfn;
5+
6+
mod mymod {
7+
pub fn hello() {
8+
println!("hello world");
9+
}
10+
}
11+
12+
#[cfg(test)]
13+
mod tests {
14+
const N: u64 = 5;
15+
16+
#[test]
17+
fn my_test() {
18+
assert!(N % 2 == 1);
19+
}
20+
}
21+
22+
fn main() {
23+
use mymod::*;
24+
hello();
25+
fancymod::fancy_hello();
26+
subfn::really_fancy_hello("me");
27+
}

modules/src/reallyfancymod/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod subfn;

modules/src/reallyfancymod/subfn.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub fn really_fancy_hello(who: &str) {
2+
println!("helloo? {}?", who);
3+
}

0 commit comments

Comments
 (0)