1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#![doc = include_str!("../README.md")]
#![doc(html_logo_url = "https://heaths.github.io/samples-rs/assets/samplesaurus_100.png")]

#[cfg(doctest)]
mod docs;
mod subjects;
#[cfg(test)]
mod test;

use std::fmt;
pub use subjects::*;

/// Say hello to whoever you specify.
///
/// # Examples
///
/// ```
/// # use samples::say_hello;
///
/// let greeting = say_hello("world");
/// assert_eq!("hello, world!", &greeting);
/// ```
pub fn say_hello(who: impl fmt::Display) -> String {
    format!("hello, {}!", &who)
}

#[test]
fn test_say_hello_num() {
    let greeting = say_hello(1.to_string());
    assert_eq!("hello, 1!", greeting);
}

/// Say hello to the world.
///
/// # Examples
///
/// ```
/// # use samples::hello_world;
///
/// let greeting = hello_world();
/// assert_eq!("hello, world!", greeting);
/// ```
pub fn hello_world() -> &'static str {
    "hello, world!"
}

#[cfg(test)]
mod tests {
    use super::*;

    #[derive(Debug)]
    struct World;

    impl fmt::Display for World {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            f.write_str("world")
        }
    }

    #[test]
    fn say_hello_world() {
        let greeting = say_hello(World);
        assert_eq!("hello, world!", &greeting);
    }
}