my_first_lib_crate/
lib.rs

1pub mod math{
2    /// Adds two numbers.
3    ///
4    ///  # Examples
5    ///
6    /// ```
7    /// use my_first_lib_crate::math::add;
8    /// assert_eq!(add(2,3),5);
9    /// ```
10    pub fn add(a: i32, b: i32) -> i32{
11        a + b
12    }
13
14    /// fist number - second number
15    pub fn sub(a: i32, b: i32) -> i32{
16        a - b
17    }
18}
19
20pub struct User{
21    name: String,
22    age: u32,
23}
24
25impl User{
26    pub fn new(name: String, age: u32) -> Self{
27        Self{name:name.to_string(), age}
28    }
29
30    /// Returns the user's name.
31    pub fn get_name(&self) -> &str {
32        &self.name
33    }
34
35    /// Returns the user's age.
36    pub fn get_age(&self) -> u32 {
37        self.age
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44    #[test]
45    fn test_add(){
46        assert_eq!(math::add(2, 2), 4);
47    }
48
49    #[test]
50    fn test_sub(){
51        assert_eq!(math::sub(5, 2), 3);
52    }
53
54    #[test]
55    fn test_user_creation(){
56        let user = User::new("John Doe".to_string(), 18);
57        assert_eq!(user.name, String::from("John Doe"));
58        assert_eq!(user.age, 18);
59    }
60}