Search
 
SCRIPT & CODE EXAMPLE
 

RUST

calculator in rust

// Add to Cargo.toml
// rprompt = "1.0.5"

fn main() {
    //Taking user input
    let a = rprompt::prompt_reply_stdout("Enter first number: ").unwrap();
    let b = rprompt::prompt_reply_stdout("Enter second number: ").unwrap();
    let cal = rprompt::prompt_reply_stdout("[1] Add [2] Subtract [3] Multiply [4] Divide: ").unwrap();

    //Turning input from string to int
    let a_int: i32 = a.parse().unwrap();
    let b_int: i32 = b.parse().unwrap();
    let cal_int: i32 = cal.parse().unwrap();

    let mut ans = 0;

    if cal_int == 1 {
        ans += a_int + b_int;
    } else if cal_int == 2 {
        ans = a_int - b_int;
    } else if cal_int == 3 {
        ans = a_int * b_int;
    } else if cal_int == 4 {
        ans = a_int / b_int;
    }

    println!("The answer is {}", ans)
}
Comment

PREVIOUS NEXT
Code Example
Rust :: rust global variables 
Rust :: enum in rust 
Rust :: ndarray rust 
Rust :: Split text on several separators 
Rust :: rust variables in println 
Rust :: custom errors rust 
Rust :: rust initialize struct 
Rust :: rust function 
Rust :: hello world in rust 
Rust :: rust•armanriazi•osstring•vs•path 
Rust :: armanriazi•rust•interior-mutability•vs•inherited-mutability 
Rust :: Read a floating point number from stdin 
Rust :: rust multiplication table for a number 
Rust :: loop label in rust 
Rust :: rust-analyzer tab space 
Rust :: armanriazi•substrate•call•dispatchable•func#ensure_signed#frame_system 
Rust :: Find unique element in array where all other elements occur 3 times, uses boolean logic 
Lua :: random string generator lua 
Lua :: lua multiline comment 
Lua :: roblox what is the difference between __index and __newindex 
Lua :: Pass values to functions from Onclicks roblox 
Lua :: lua metatable assignment 
Lua :: Startswith function in lua 
Lua :: lua table unpack 
Lua :: how to make text different colors in LUA terminal 
Lua :: roblox lua how to apply gravity to a object 
Lua :: pico8 draw dot 
Lua :: lagstep roblox 
Matlab :: matlab measure time 
Matlab :: matlab make last value the first one etc 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =