Menu
CoddyTech

Boolean Conditions

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

GDScript boolean conversion treats an empty Array as false, so the conditional selects fallback. Rust conditions require bool and do not implicitly convert collections or numeric lengths. Write the intended fact explicitly with is_empty, a comparison, or an Option match. GDScript and and or are boolean operations; do not assume they form an operand-returning fallback expression. The Rust example checks emptiness and then short-circuits before an otherwise invalid index read. The right side of && is skipped when the left side is false. Missing data, zero values, and empty containers are separate states even when source-language truthiness can combine them.

GDScript: complete program

extends SceneTree

func _init():
    var items = []
    var chosen = "present" if items else "fallback"
    print(chosen)
    print(true if items else false)
    quit()

Rust: complete program

fn main() {
    let items: Vec<i32> = Vec::new();
    let present = !items.is_empty();
    println!("{}", present);
    println!("{}", present && items[0] == 9);
}

An explicit emptiness predicate preserves the intended meaning when translating a GDScript condition into Rust.

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 GDScript Developers

Practice on your own: Online Rust compiler