Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

Comparison Questions - Part 1
RUST

Comparison Questions - Part 1

Practice 15 Rust comparison questions covering equality, ordering, references, ownership, and value comparisons.

1. What does this == comparison print?

let a: i32 = 5;
let b: i32 = 5;
let c: i32 = 6;
println!("{}", a == b);
println!("{}", a == c);
println!("{}", a < c);

Output:

true
false
true

5 == 5true, 5 == 6false, 5 < 6true. Numeric comparison is value-based. == works because i32 implements PartialEq; all primitive types do.

2. What does this == on strings print?

let a = String::from("abc");
let b = String::from("abc");
let c = String::from("abd");
println!("{}", a == b);
println!("{}", a == c);
println!("{}", a < c);

Output:

true
false
true

String implements PartialEq for content comparison → "abc" == "abc" is true. "abc" < "abd" compares lexicographically → true. String equality is content-based.

3. What does the == on String vs &str print?

let s = String::from("hello");
let t: &str = "hello";
println!("{}", s == t);
println!("{}", t == "hello");
println!("{}", s == "hello");

Output:

true
true
true

Rust provides cross-type PartialEq impls: String == &str, &str == &str, String == "literal". All compare contenttrue each time. Note this is content equality even between String and &str — there’s no pointer/reference equality for strings like in some languages.

4. What does this == on chars print?

println!("{}", 'a' == 'a');
println!("{}", 'a' == 'A');
println!("{}", 'a' < 'b');
println!("{}", 'A' < 'a');

Output:

true
false
true
true

Chars are Unicode code points. 'a' and 'A' differ (97 vs 65). 'a' < 'b' since 97 < 98. 'A' < 'a' since 65 < 97. Character comparison is code-point (numeric) comparison, and ==/< are PartialEq/PartialOrd for char.

5. What does this == on floating point print?

let x = 0.5f64;
let y = 0.5f64;
let a = 0.1f64;
let b = 0.2f64;
let c = 0.3f64;
println!("{}", x == y);
println!("{}", a + b == c);
println!("{}", f64::NAN == f64::NAN);

Output:

true
false
false

0.5 is exactly representable → true. 0.1 + 0.2 is 0.30000000000000004, not 0.3false. And NAN == NAN is false — NaN is not equal to itself. This is why floats need epsilon comparison, and NaN can’t sit in a HashMap/HashSet key. (Note: f64 doesn’t implement Eq, only PartialEq.)

6. What does this == on Option print?

let a = Some(10);
let b = Some(10);
let c = Some(20);
let d: Option<i32> = None;
println!("{}", a == b);
println!("{}", a == c);
println!("{}", d == None);

Output:

true
false
true

Option implements PartialEq when its inner type does (as i32 does). Some(10) == Some(10)true. Some(10) == Some(20)false. None == Nonetrue. Options compare by both variant and payload.

7. What does this == on Vec print?

let v1 = vec![1, 2, 3];
let v2 = vec![1, 2, 3];
let v3 = vec![1, 2, 4];
println!("{}", v1 == v2);
println!("{}", v1 == v3);
println!("{}", v1.len() == v3.len());

Output:

true
false
true

Vec implements PartialEq by comparing element by element. [1,2,3] == [1,2,3]true. [1,2,3] == [1,2,4]false. Length is the same (3), but the third element differs.

8. What does this == on array vs slice print?

let arr = [1, 2, 3];
let arr2 = [1, 2, 3];
let slice: &[i32] = &arr;
println!("{}", arr == arr2);
println!("{}", slice == arr2);
println!("{}", slice == [1, 2, 3]);

Output:

true
true
true

Arrays and slices compare element-wise. [1,2,3] == [1,2,3]true. &[1,2,3] == [1,2,3] compares the slice’s elements → true. slice == [1,2,3] compares against a fixed-size array literal → true. Comparison is by elements, regardless of array vs slice.

9. What does this == on tuple print?

let t1 = (1, "a");
let t2 = (1, "a");
let t3 = (1, "b");
println!("{}", t1 == t2);
println!("{}", t1 == t3);

Output:

true
false

Tuples compare element-wise, in the same order, and only if every element type is PartialEq. (1, "a") == (1, "a")true. (1, "a") == (1, "b")"a" != "b"false.

10. What does this == on reference print?

let x: i32 = 5;
let r1: &i32 = &x;
let r2: &i32 = &x;
println!("{}", r1 == r2);
println!("{}", *r1 == *r2);

Output:

true
true

&i32 == &i32 compares the pointed-to values5 == 5true. In Rust, PartialEq for references delegates to the referent. *r1 == *r2 dereferences first → also true. (There is no concept of pointer-equality by address in safe Rust comparison.)

11. What does this == on different integer types print?

let a: i32 = 5;
let b: i64 = 5;
println!("{}", a == b);

Output:

Compile error: mismatched types, expected `i32`, found `i64`

i32 and i64 are different, non-coercible types — there’s no cross-type PartialEq<i64> for i32. Rust does not implicitly convert ints, so a == b is a compile error. You must convert explicitly: (a as i64) == b.

12. What does this == on usize and i32 print?

let n: usize = 4;
let m: i32 = 4;
println!("{}", n == m);

Output:

Compile error: mismatched types

Same as above — usize and i32 are distinct integer types. Comparison fails to compile. Convert with as: (n as i64) == (m as i64) or compare n == m as usize.

13. What does this == on str content print?

let a: &str = "hello";
let b: &str = "hello";
let c: &str = "world";
let mut hs1 = a;
hs1 = "world";
println!("{}", a == b);
println!("{}", a == c);
println!("{}", hs1 == c);

Output:

true
false
true

&str == &str compares content: "hello" == "hello"true, "hello" == "world"false, and after reassigning hs1 to "world", "world" == "world"true. &str comparison is all about the pointed-to text.

14. What does this == on result of function print?

fn double(n: i32) -> i32 {
    n * 2
}
println!("{}", double(4) == 8);
println!("{}", double(4) == double(4));
println!("{}", double(4) < double(5));

Output:

true
true
true

double(4) is 8== 8true. 8 == 8true. 8 < 10true. Returned values compare normally — the function call is evaluated once per position.

15. What does this == on struct print?

#[derive(PartialEq)]
struct Point {
    x: i32,
    y: i32,
}
let p1 = Point { x: 1, y: 2 };
let p2 = Point { x: 1, y: 2 };
let p3 = Point { x: 1, y: 3 };
println!("{}", p1 == p2);
println!("{}", p1 == p3);

Output:

true
false

For structs you must derive PartialEq (unlike primitives). With it, p1 == p2 compares field by field → true. p1 == p3 differs in yfalse. Without #[derive(PartialEq)], this wouldn’t compile.

My Private Notes

Notes are auto-saved locally to this device.