This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Full tutorial here https://youtu.be/LJppDNH2CHI | |
#[cfg(test)] | |
mod test{ | |
use crate::Calculator; | |
#[test] | |
fn test_addition(){ | |
let mut calculator = Calculator::new(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cargo new project_name | |
// dailydoseofrust.com |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Full tutorial - https://dailydoseofrust.com/dependency-management-in-rust-cargo-toml-file-with-examples-and-video/ | |
[dependencies] | |
crate_name = "0.1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"name": "STANDARD TRACKED SHIPPING (7-15 Business Days)" | |
}, | |
{ | |
"name": "EXPRESS TRACKED SHIPPING (5-10 Business Days)" | |
}, | |
{ | |
"name": "BULK AIR FREIGHT (10-20 Business Days)" | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"name": "Women's Fashion", | |
"children": [ | |
{ | |
"name": "Women Top Clothing", | |
"children": [ | |
] | |
}, | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. | |
} |
NewerOlder