test_cp_first_lib 0.1.0

a first lib of study rust lib
Documentation
/// Adds two numbers together.
///
/// # Arguments
///
/// * `x` - The first number to add.
/// * `y` - The second number to add.
///
/// # Returns
///
/// The sum of `x` and `y`.
///
/// # Examples
///
/// ```
/// use test_cp_first_lib::add_num;
/// let result = add_num(1, 2);
/// assert_eq!(result, 3);
/// ```
pub fn add_num(x: i32, y: i32) -> i32 {
    x + y
}

/// `say_hi` Prints a greeting to the console.
///
/// # Arguments
///
/// * `name` - The name to greet.
///
/// # Examples
///
/// ```
/// use test_cp_first_lib::say_hi;
/// say_hi("Alice");    // Prints "Hi Alice!"
/// say_hi("Bob");      // Prints "Hi Bob!"
/// ```
///
/// # Panics
///
/// 如果输入是异常值,则此函数会 panic。
///
/// # Errors
///
/// 这个函数不会返回 `Result`,所以没有错误。
///
/// # Safety
///
/// 这个函数是安全的,不需要额外的前置条件。
///
pub fn say_hi(name: &str) {
    println!("Hi {name}!");
}

/// 从字符串中解析一个整数。
///
/// # Arguments
///
/// * `s` - 要解析的字符串。
///
/// # Returns
///
/// 如果解析成功,返回解析后的整数;否则返回错误。
///
/// # Errors
///
/// 如果字符串无法正确解析为整数,会返回 `std::num::ParseIntError`。
///
/// # Panics
///
/// 无。
///
/// # Safety
///
/// - 安全,没有不安全代码。
///
/// # Examples
///   
/// ```    
/// use test_cp_first_lib::parse_number;
/// let result = parse_number("42");
/// assert_eq!(result, Ok(42));
///     
/// let result = parse_number("not_a_number");
/// assert!(result.is_err());
///     
///  ```
pub fn parse_number(s: &str) -> Result<i32, std::num::ParseIntError> {
    s.parse()
}

/// 将一个原始指针转换为可变引用。
///
/// # Safety
///
/// 调用者必须保证:
/// - `ptr` 必须是一个有效的、对齐的、初始化了的 `i32` 类型的指针。
/// - 在同一时间,不能有任何别的可变引用或者不可变引用指向同一块内存(符合 Rust 的别名规则)。
///
/// 如果违反以上任一条件,行为是未定义的(UB)。
///
/// # Examples
///
/// ```
/// use test_cp_first_lib::ptr_to_mut_ref;
///
/// let mut num = 42;
/// let ptr = &mut num as *mut i32;
///
/// let ref_to_num = unsafe { ptr_to_mut_ref(ptr) };
/// *ref_to_num = 24;
///
/// assert_eq!(num, 24);
///     
/// ```
pub unsafe fn ptr_to_mut_ref(ptr: *mut i32) -> &'static mut i32 {
    &mut *ptr
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn add_1_to_2_is_3() {
        let result = add_num(1, 2);
        assert_eq!(result, 3);
    }
}

/// 测试 `ptr_to_mut_ref` 函数的!标识。
///
/// ```
/// use test_cp_first_lib::ptr_to_mut_ref;
///
/// let mut num = 42;
/// let ptr = &mut num as *mut i32;
///
/// let ref_to_num = unsafe { ptr_to_mut_ref(ptr) };
/// *ref_to_num = 24;
///
/// assert_eq!(num, 24);
///     
/// ```
pub unsafe fn ptr_to_mut_ref2(ptr: i32) -> i32 {
    ptr
}

pub struct Person {
    name: String,
    age: u8,
}

pub enum Color {
    Red,
    Yellow,
    Blue,
}

pub mod kinds {
    /// The primary colors according to the RYB color model.
    pub enum PrimaryColor {
        Red,
        Yellow,
        Blue,
    }

    /// The secondary colors according to the RYB color model.
    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 {
        // --snip--
        unimplemented!();
    }
}

pub use self::kinds::SecondaryColor;
pub use self::utils::mix;
pub use crate::kinds::PrimaryColor;