Skip to content

Commit

Permalink
Add docstring rust projects app
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-saad-la committed Jul 20, 2024
1 parent ba159a3 commit 4767d5e
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
7 changes: 7 additions & 0 deletions code/01_starter/docstring_projects/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions code/01_starter/docstring_projects/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "docstring_projects"
version = "0.1.0"
edition = "2021"

[dependencies]
122 changes: 122 additions & 0 deletions code/01_starter/docstring_projects/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
//================================================================================
// By: Dr. Saad Laouadi
// Course: Rust Distilled
// Lesson: Documenting Rust Projects
//
// © copy-right: Dr. Saad Laouadi
//================================================================================

//! This module provides utility functions for mathematical operations.
//! It includes basic arithmetic operations such as addition and multiplication.
//! This documentation is generated using `//!` syntax
/*!
*
* This module provides utility functions for mathematical operations.
* It includes basic arithmetic operations such as addition and multiplication.
* This is also an inner documentation but generated using the `/*! ... */` syntax
*
*/

/*!
* Generate Code Documentation in Rust
* ===================================
*
* To generate the documentation, run the following command:
*
* ```
* cargo doc --open
* ```
*
* This command will compile your code and generate the documentation, then open it in your default web browser.
*
* Public and Private Functions
* ============================
*
* In Rust, functions are private by default. A private function is indicated by a lock icon in the generated documentation.
* To make a function accessible from outside its module, you need to mark it with the `pub` keyword.
*
* Example:
*
* ```rust
* pub fn public_function() {
* // This function is public
* }
*
* fn private_function() {
* // This function is private
* }
* ```
*
* The lock icon in the generated documentation signifies that the function is private and not accessible from outside the module.
*/

/// Adds two numbers and returns the result.
///
/// # Arguments
///
/// * `a` - The first number.
/// * `b` - The second number.
///
/// # Example
///
/// ```
/// let result = add(5, 3);
/// println!("The result is {}", result); // The result is 8
/// ```
pub fn add(a: i32, b: i32) -> i32 {
a + b
}

/// Subtracts the second number from the first and returns the result.
///
/// # Arguments
///
/// * `a` - The first number.
/// * `b` - The second number.
///
/// # Example
///
/// ```
/// let result = subtract(5, 3);
/// println!("The result is {}", result); // The result is 2
/// ```
fn subtract(a: i32, b: i32) -> i32 {
a - b
}

/// Multiplies two numbers and returns the result.
///
/// # Arguments
///
/// * `a` - The first number.
/// * `b` - The second number.
///
/// # Example
///
/// ```
/// let result = multiply(5, 3);
/// println!("The result is {}", result); // The result is 15
/// ```
pub fn multiply(a: i32, b: i32) -> i32 {
a * b
}

/// The main function serves as the entry point for the program.
/// It demonstrates the usage of the `add` function and prints results to the console.
///
/// # Examples
///
/// ```
/// main();
///
fn main() {
println!("{}", "*".repeat(52));
let result = add(5, 3);
println!("The result of adding 5 and 3 is: {}", result); // The result is 8
println!("{}", "*".repeat(52));

let result_subtract = subtract(5, 3);
println!("The result of subtracting 3 from 5 is: {}", result_subtract); // The result is 2
println!("{}", "*".repeat(52));
}

0 comments on commit 4767d5e

Please sign in to comment.