To check if two vectors have the same items regardless of order in Rust, you can sort both vectors and then compare them.
In this example,
vector1
and vector2
.sort()
method from the Vec
type.==
operator to check if they are equal.==
operator returns true
if the vectors have the same items regardless of order, otherwise false
.let mut vector1 = vec![3, 1, 2];
let mut vector2 = vec![1, 2, 3];
vector1.sort();
vector2.sort();
let are_equal = vector1 == vector2;
println!("{:?}", are_equal);
true
In this example,
vector1
and vector2
.sort()
method from the Vec
type.==
operator to check if they are equal.==
operator returns true
if the vectors have the same items regardless of order, otherwise false
.let mut vector1 = vec![b'b', b'c', b'a'];
let mut vector2 = vec![b'a', b'b', b'c'];
vector1.sort();
vector2.sort();
let are_equal = vector1 == vector2;
println!("{:?}", are_equal);
true
In this tutorial, we learned How to check if Two Vectors have Same Items (Regardless of Order) in Rust language with well detailed examples.