Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the minimum value within a vector in Rust?

Tags:

rust

I'm trying to display the minimum value within a vector in Rust and can't find a good way to do so.

Given a vector of i32 :

let mut v = vec![5, 6, 8, 4, 2, 7];

My goal here is to get the minimum value of that vector without having to sort it.

What is the best way to get the minimum value within a Vec<i32> in Rust ?

like image 942
octano Avatar asked Nov 02 '19 09:11

octano


People also ask

How do vectors work in Rust?

Vector is a module in Rust that provides the container space to store values. It is a contiguous resizable array type, with heap-allocated contents. It is denoted by Vec<T>. Vectors in Rust have O(1) indexing and push and pop operations in vector also take O(1) complexity.

How do you make an empty vector in Rust?

In Rust, there are several ways to initialize a vector. In order to initialize a vector via the new() method call, we use the double colon operator: let mut vec = Vec::new(); This call constructs a new, empty Vec<T> .

How to find the minimum value of a vector in R?

The min () function in R computes the minimum value of a vector or data frame. The min () is a built-in R function that takes an object as an input and returns the minimum value out of it. To find the minimum value of vector elements, data frame, and columns, use the min () function. input: It is a Vector or a data frame.

How to find the minimum and maximum element of a vector?

Given a vector, find the minimum and maximum element of this vector using STL in C++. Example: Approach: Min or Minimum element can be found with the help of *min_element() function provided in STL.

What is the use of vector in rust?

Vector is a module in Rust that provides the container space to store values. It is a contiguous resizable array type, with heap-allocated contents. It is denoted by Vec<T>. Vectors in Rust have O (1) indexing and push and pop operations in vector also take O (1) complexity.

What is the minimum value of the missing data vector?

You can see from the vector that mathematically, the minimum value is -46, and we get the same output. If you add the NA value as missing data to the vector, it will return the NA value as a minimum value because it is not what the NA value is because it can be anything. So it will assume the minimum value as NA returns that in the output.


3 Answers

let minValue = vec.iter().min();
match minValue {
    Some(min) => println!( "Min value: {}", min ),
    None      => println!( "Vector is empty" ),
}

https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.min

fn min(self) -> Option<Self::Item>
where
    Self::Item: Ord, 

Returns the minimum element of an iterator.

If several elements are equally minimum, the first element is returned. If the iterator is empty, None is returned.

I found this Gist which has some common C#/.NET Linq operations expressed in Swift and Rust, which is handy: https://gist.github.com/leonardo-m/6e9315a57fe9caa893472c2935e9d589

like image 185
Dai Avatar answered Oct 17 '22 22:10

Dai


let mut v = vec![5, 6, 8, 4, 2, 7];
let minValue = *v.iter().min().unwrap();
like image 21
thouger Avatar answered Oct 17 '22 20:10

thouger


Hi @octano As Dai has already answered, min/max return Option<> value, so you can only match it as in example:

fn main() {
    let vec_to_check = vec![5, 6, 8, 4, 2, 7];
    let min_value = vec_to_check.iter().min();
    match min_value {
        None => println!("Min value was not found"),
        Some(i) => println!("Min Value = {}", i)
    }
}

Play ground example for Iter.min()

like image 5
blandger Avatar answered Oct 17 '22 22:10

blandger