mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-08-11 22:14:47 +00:00
add chapter [Docs and Comments]
This commit is contained in:
36
practices/doc-comments/src/compute.rs
Normal file
36
practices/doc-comments/src/compute.rs
Normal file
@@ -0,0 +1,36 @@
|
||||
//! Do some complicated arithmetic that you can't do by yourself
|
||||
|
||||
/// # Panics
|
||||
///
|
||||
/// The function panics if the second argument is zero.
|
||||
///
|
||||
/// ```rust,should_panic
|
||||
/// // panics on division by zero
|
||||
/// doc_comments::compute::div(10, 0);
|
||||
/// ```
|
||||
pub fn div(a: i32, b: i32) -> i32 {
|
||||
if b == 0 {
|
||||
panic!("Divide-by-zero error");
|
||||
}
|
||||
|
||||
a / b
|
||||
}
|
||||
|
||||
|
||||
/// ```
|
||||
/// # fn try_main() -> Result<(), String> {
|
||||
/// let res = doc_comments::compute::try_div(10, 1)?;
|
||||
/// # Ok(()) // returning from try_main
|
||||
/// # }
|
||||
/// # fn main() {
|
||||
/// # try_main().unwrap();
|
||||
/// #
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn try_div(a: i32, b: i32) -> Result<i32, String> {
|
||||
if b == 0 {
|
||||
Err(String::from("Divide-by-zero"))
|
||||
} else {
|
||||
Ok(a / b)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user