Menu
CoddyTech

Boolean Conditions

Lesson 7 of 48 in Coddy's Rust for JavaScript Developers course.

JavaScript conditions run on truthiness: 0, "", NaN, null and undefined all select the false branch, and everything else selects the true one. Its logical operators also return one of their operands, so label || "fallback" yields a string rather than a boolean. Rust conditions require bool; a number or string is never converted implicitly. Rust comparisons return booleans, && and || short-circuit and always produce bool, and ! on an integer flips its bits instead of testing for zero. Equality also differs. JavaScript has loose ==, which coerces, and strict ===; Rust has only ==, which compares two values of compatible types and refuses to compare a number with a string. The examples test an empty label and a zero count, first with truthiness and then with explicit tests. Avoid carrying the truthiness shortcut into Rust. Name the property you intend to test, such as emptiness or equality, and make that comparison visible. This also avoids confusing a missing value with a valid zero.

Complete standalone JavaScript program:

const label = "";
const count = 0;
console.log(label || "fallback");
console.log(!label && !count);

JavaScript output:

fallback
true

Complete standalone Rust program:

fn main() {
    let label = "";
    let count = 0;
    println!("{}", label.is_empty());
    println!("{}", label.is_empty() && count == 0);
}

Rust output:

true
true
challenge icon

Challenge

Easy

Decide whether a machine may run. Input is temperature door emergency, with temperature from -100 to 200 and both flags either 0 or 1. A closed door is door=1; an emergency is emergency=1. Required API: pub fn temperature_ok(value: i32) -> bool accepts the inclusive interval 10 through 80; pub fn may_run(temperature: i32, door_closed: bool, emergency: bool) -> bool requires an acceptable temperature, a closed door, and no emergency. Output the two results as lowercase Rust booleans separated by one space. Implement the public API in safety.rs. The supplied exercise.rs parses valid input and calls that API. Return the specified text without a trailing newline; the locked driver adds one newline. Do not print from the API. All input values satisfy the stated bounds.

Implement the required exported APIs and return the result from exercise::solve(input: &str) -> String without a final newline. Do not print in editable code. The locked driver checks required APIs independently, reads all stdin, and prints the returned text with one final newline.

Your code goes in exercise.rs, safety.rs. main.rs holds the supplied input/output code and cannot be edited.

Try it yourself

#![allow(dead_code, unused_imports, unused_variables, unused_mut)]
mod exercise;
mod safety;
use std::io::{self, Read};
fn main() {
    let mut input=String::new();
    io::stdin().read_to_string(&mut input).unwrap();
    // BEGIN PUBLIC API PROBES
assert!(safety::temperature_ok(10));
assert!(safety::temperature_ok(80));
assert!(!safety::temperature_ok(9));
assert!(!safety::temperature_ok(81));
assert!(safety::may_run(47, true, false));
assert!(!safety::may_run(47, false, false));
assert!(!safety::may_run(47, true, true));
assert!(!safety::may_run(-1, true, false));
assert!(safety::may_run(47, true, false));
    // END PUBLIC API PROBES
    println!("{}", exercise::solve(&input));
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Rust for JavaScript Developers

Practice on your own: Online Rust compiler