16. What does this == on HashMap with str keys print?
use std::collections::HashMap;
let mut m = HashMap::new();
m.insert("a", 10);
m.insert("b", 20);
println!("{}", m.get("a") == Some(&10));
println!("{}", m.contains_key(&"b"));
println!("{}", m.is_empty());
Output:
true
true
false
m.get("a") returns Some(&10). Comparing Some(&10) == Some(&10) → true. m.contains_key(&"b") → true. The map has 2 entries, so is_empty() → false. Note: get returns a reference (&V), so you compare against Some(&10).
17. What does this == on HashMap .get missing key print?
use std::collections::HashMap;
let m: HashMap<String, i32> = HashMap::new();
println!("{}", m.get("x") == None);
println!("{}", m.is_empty());
Output:
true
true
Reading a missing key returns None → None == None → true. The map is empty → true. Rust maps don’t return zero values for missing keys; they return Option, so a missing key is distinguishable from a present one with value 0.
18. What does this == on string chars and bytes print?
let s: &str = "hi";
let ch = s.chars().next();
println!("{:?}", ch);
println!("{}", ch == Some('h'));
println!("{}", s.as_bytes().len() == 2);
Output:
Some('h')
true
true
s.chars() yields chars; .next() gives the first → Some('h'). ch == Some('h') → true (Some equality unwraps the payload). s.as_bytes().len() is 2 bytes → == 2 → true.
19. What does this == on floats where one is a variable print?
let x: f64 = 0.1;
let y: f64 = 0.1;
let z: f64 = 0.3;
println!("{}", x == y);
println!("{}", x + x + x == z);
println!("{}", (x - y) == 0.0);
Output:
true
false
true
0.1 == 0.1 (same binary double) → true. 0.1 + 0.1 + 0.1 accumulates to 0.30000000000000004, not 0.3 → false. x - y is exactly 0.0 (subtracting a value from itself) → true. Identity ops are exact; accumulation drifts.
20. What does this == on an optional string print?
let a: Option<String> = Some("hi".to_string());
let b: Option<String> = Some("hi".to_string());
let c: Option<String> = Some("yo".to_string());
println!("{}", a == b);
println!("{}", a == c);
Output:
true
false
Option<String> compares both variant and inner String content. Some("hi") == Some("hi") → true. Some("hi") == Some("yo") → false.
21. What does this == on &String vs String print?
let s = String::from("rust");
let r = &s;
let t = String::from("rust");
println!("{}", *r == t);
println!("{}", r == &t);
println!("{}", r == s.as_str());
Output:
true
true
true
*r == t — dereference to String, compare content → true. r == &t compares two &Strings → delegates to content → true. r == s.as_str() compares &String vs &str (via PartialEq<&str> for &String) → true. References compare by content, not by address.
22. What does this == on result of get_or_insert produce?
use std::collections::HashMap;
let mut m = HashMap::new();
m.insert("k", 0);
let v = m.entry("k").or_insert(5);
println!("{}", *v == 0);
Output:
true
m.entry("k").or_insert(5) — since key "k" already exists (with value 0), or_insert returns a mutable ref to the existing value. *v == 0 → true. The default 5 is ignored because the key was present.
23. What does this == on matches! produce?
let x: Option<i32> = Some(10);
println!("{}", matches!(x, Some(10)));
println!("{}", matches!(x, Some(_)));
println!("{}", matches!(x, None));
Output:
true
true
false
matches! returns bool by pattern-matching. Some(10) matches Some(10) → true. It also matches Some(_) → true. It does not match None → false. matches! is a macro that produces a boolean directly.
24. What does this == on two mutable refs print?
let mut x = 5;
let r1 = &mut x;
*r1 = 10;
println!("{}", x == 10);
let r2 = &mut x;
*r2 = 15;
println!("{}", x == 15);
Output:
true
true
r1 mutably borrows x, sets it to 10 → x == 10 → true. After r1 is done, r2 mutably borrows and sets 15 → x == 15 → true. Rust only allows one active mutable borrow at a time — that’s why the two borrows don’t overlap here (they’re sequential).
25. What does this == on Compare with different numeric types print?
let a: u32 = 300;
let b: u8 = 200;
println!("{:?}", a.cmp(&(b as u32)));
println!("{}", a > b as u32);
Output:
Greater
true
cmp needs both sides the same type, so b as u32. 300.cmp(200) → Ordering::Greater (prints as Greater). 300 > 200 → true. Ordering is the three-way enum Less / Equal / Greater.
26. What does this == on Result produce?
let r1: Result<i32, &str> = Ok(10);
let r2: Result<i32, &str> = Ok(10);
let r3: Result<i32, &str> = Err("boom");
println!("{}", r1 == r2);
println!("{}", r1 == r3);
println!("{}", r1.is_ok());
Output:
true
false
true
Result is PartialEq when both T and E are. Ok(10) == Ok(10) → true. Ok(10) == Err("boom") → false (different variant). r1.is_ok() → true. Results compare by variant and payload.
27. What does == on HashSet membership print?
use std::collections::HashSet;
let mut s = HashSet::new();
s.insert(1);
s.insert(2);
println!("{}", s.contains(&1));
println!("{}", s.contains(&3));
println!("{}", s.len() == 2);
Output:
true
false
true
contains(&1) → true. contains(&3) → false. len() == 2 → true. Note: HashSet values must implement Eq (not just PartialEq) — so f64 and f32 are not allowed in a HashSet (NaN and precision issues). That’s why the earlier float questions couldn’t be hashed.
28. What does this == on universal equality print?
#[derive(Copy, Clone, PartialEq)]
enum Color {
Red,
Blue,
}
let c1 = Color::Red;
let c2 = Color::Red;
let c3 = Color::Blue;
println!("{}", c1 == c2);
println!("{}", c1 == c3);
Output:
true
false
Enum variants derive PartialEq (via #[derive(PartialEq)]). Red == Red → true. Red == Blue → false. Without the derive, == wouldn’t compile for a user-defined enum.
29. What does this == on a borrowed return print?
fn first(s: &str) -> char {
s.chars().next().unwrap()
}
let name = String::from("zoom");
let c = first(&name);
println!("{}", c == 'z');
println!("{}", name.starts_with('z'));
Output:
true
true
first(&name) returns 'z' → == 'z' → true. name.starts_with('z') → true. The function borrows name (takes &str), so name is still usable after the call — no ownership was moved.
30. What does this == on range include check print?
let r = 1..=5;
println!("{}", r.contains(&3));
println!("{}", r.contains(&5));
println!("{}", r.contains(&6));
Output:
true
true
false
r.contains(&3) → true. 1..=5 is inclusive, so contains(&5) → true. contains(&6) → false. Unlike the exclusive 1..5 (which excludes 5), the inclusive range includes both endpoints.
Premium Content
Unlock Comparison Questions - Part 2 and all premium lessons with a subscription.
From ₹199.99/year — See plans