Search
 
SCRIPT & CODE EXAMPLE
 

RUST

rust option

//Type Option represents an optional value: every Option is either Some and contains a value, or None, and does not.
//Option is an enum consisting of Some and None

fn divide(numerator: f64, denominator: f64) -> Option<f64> {
    if denominator == 0.0 {
        None
    } else {
        Some(numerator / denominator)
    }
}

// The return value of the function is an option
let result = divide(2.0, 3.0);

// Pattern match to retrieve the value
match result {
    // The division was valid
    Some(x) => println!("Result: {x}"),
    // The division was invalid
    None    => println!("Cannot divide by 0"),
}
Comment

PREVIOUS NEXT
Code Example
Rust :: rust const string 
Rust :: rust array unique 
Rust :: new rust project with cargo 
Rust :: optional arguments rust 
Rust :: struct in rust 
Rust :: read file in rust bufreader 
Rust :: run or compile rust code 
Rust :: transpose a matrix 
Rust :: rust lang enum 
Rust :: armanriazi•rust•error•[E0106]: missing lifetime specifier -- src/main.rs:5:16 | 5 | fn dangle() - &String { | ^ expected named lifetime parameter 
Rust :: armanriazi•rust•error•cannot use the `?` operator in a function that returns `()` 
Rust :: rust BMI 
Rust :: armanriazi•rust•mem•deallocating 
Rust :: armanriazi•rust•comparison•iter•vs•for 
Rust :: rust sort vec of f64 
Rust :: armanriazi•rust•nestedtypes 
Rust :: rust•armanriazi•capacity•reserve 
Rust :: rust string split 
Lua :: lua not equal 
Lua :: lua How to remove index from table 
Lua :: gfjhhkn roblox 
Lua :: loop true childs roblox 
Lua :: lua list append 
Lua :: What is transparency in roblox 
Lua :: lua function 
Lua :: lua concatenation 
Lua :: lua loop through string 
Lua :: how to check if table is clear 
Matlab :: matlab string to int 
Matlab :: matlab 1d matrix declarationg 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =