Skip to content

Instantly share code, notes, and snippets.

View henryobiaraije's full-sized avatar
🏠
Working from home

Henry Obiaraije henryobiaraije

🏠
Working from home
View GitHub Profile
@henryobiaraije
henryobiaraije / lib.rs
Created May 19, 2023 20:56
Writing unit test for the Calculator struct.
// Full tutorial here https://youtu.be/LJppDNH2CHI
#[cfg(test)]
mod test{
use crate::Calculator;
#[test]
fn test_addition(){
let mut calculator = Calculator::new();
@henryobiaraije
henryobiaraije / main.rs
Created May 19, 2023 20:48
How to use the Calculator Crate
// Full video tutorial here https://www.youtube.com/watch?v=LJppDNH2CHI
use calculator::Calculator;
fn main(){
let mut calculator = Calculator::new();
calculator
.add([4.0,10.0,20.0].to_vec())
.subsctract([100.0].to_vec());
@henryobiaraije
henryobiaraije / lib.rs
Created May 19, 2023 20:00
Calculator code in the calculator library crate.
// Full tutorial https://www.youtube.com/watch?v=LJppDNH2CHI
pub struct Calculator {
result: f64,
}
impl Calculator {
/// Creates a new Calculator instance with the initial result set to 0.0.
pub fn new() -> Self {
return Self { result: 0.0 };
@henryobiaraije
henryobiaraije / rust.rs
Created May 19, 2023 19:40
How to Create a new Rust Cargo Project
cargo new project_name
// dailydoseofrust.com
@henryobiaraije
henryobiaraije / cargo.toml
Last active May 18, 2023 18:56
How to import crates from crates.io for rust cargo
# Full tutorial - https://dailydoseofrust.com/dependency-management-in-rust-cargo-toml-file-with-examples-and-video/
[dependencies]
crate_name = "0.1.0"
[
{
"name": "STANDARD TRACKED SHIPPING (7-15 Business Days)"
},
{
"name": "EXPRESS TRACKED SHIPPING (5-10 Business Days)"
},
{
"name": "BULK AIR FREIGHT (10-20 Business Days)"
},
[
{
"name": "Women's Fashion",
"children": [
{
"name": "Women Top Clothing",
"children": [
]
},
{
@henryobiaraije
henryobiaraije / main.rs
Created May 15, 2023 22:34
Full Code - Basic Unit Testing in Rust
fn main() {
let result1 = divide(4.0, 0.0); // Calling the divide function with arguments 4.0 and 0.0 and storing the result in result1.
println!("result1 is {:?}", result1); // Printing the value of result1 using debug formatting.
}
fn divide(a: f64, b: f64) -> Result<f64, String> {
if 0.0 == b {
return Result::Err("Hey, you can't divide by zero".to_string()); // Checking if b is equal to zero. If true, return an Err variant of the Result enum with an error message.
}
@henryobiaraije
henryobiaraije / main.rs
Created May 15, 2023 22:30
Rust test module and test function to unit test a function.
#[cfg(test)]
mod test_mod {
use super::divide; // Importing the divide function from the outer scope.
#[test]
fn test_divide_2_values() {
let result1 = divide(4.0, 2.0); // Calling the divide function with arguments 4.0 and 2.0 and storing the result in result1.
assert_eq!(result1, Result::Ok(2.0)); // Asserting that the value of result1 is equal to an Ok variant of the Result enum with a value of 2.0.
@henryobiaraije
henryobiaraije / main.rs
Created May 15, 2023 21:46
A function in Rust language to divide one number by another.
// Full tutorial : https://daily-dose-of-rust-language.pereere.com/2023/05/15/a-simple-example-of-error-handling-and-testing-in-rust/
fn divide(a: f64, b: f64) -> Result<f64, String> {
if 0.0 == b {
return Result::Err("Hey, you can't divide by zero".to_string()); // Checking if b is equal to zero. If true, return an Err variant of the Result enum with an error message.
}
return Result::Ok(a / b); // If the condition is false, return an Ok variant of the Result enum with the result of dividing a by b.
}