How to handle index out of bounds errors with multidimensional vectors

if (outside == false) {
                    rett = vec!(
                      /*  (100 as f64) as u8,
                        (255.0f64 * d as f64 / 28.0f64 as f64) as u8,
                        (200.0 as f64 * vec3_dot.abs()) as u8,*/
                         image[(u*1024.0) as usize][(v*1024.0) as usize][0]
                         image[(u*1024.0) as usize][(v*1024.0) as usize][1],
                         image[(u*1024.0) as usize][(v*1024.0) as usize][2]

image is of type &Vec<Vec<[u8;3]>>
image[(u*1024.0) as usize] and [(v*1024.0) as usize] both can cause index out of bounds errors.
When there is a error I want it to print u and v.
What should be the code to handle this type of error because using get() and match would be too long ? My first guess would be something like try and expect blocks of java.

What is the proper way to handle this in rust?

edit:I just decided to check if it fits in bounds before entering u and v in the vectors with a if loop. It's only a personal project so it is fine.

You can put the indexing in a function, then use the ? operator. Also, it is wasteful to index 3 times just to construct a 3-element vector, or to construct a vector when you have an array, so that will shorten things up a lot.

fn get2d(image: &Vec<Vec<[u8; 3]>>, u: f64, v: f64) -> Option<[u8; 3]> {
    image.get((u*1024.0) as usize)?.get((v*1024.0) as usize)


if !outside {
    rett = get2d(&image, u, v).unwrap();

Then to provide the error reporting you want, you could write another function that calls this one and transforms the Option into a Result with the coordinates in the error value.

However, Vec<Vec<T>> is an inefficient data structure which you should not use for storing images (because it scatters the rows of the image across many small allocations, and requires following a pointer to the row every time). You should instead use a single “flat” vector and compute the single index from the 2D position.

Instead of implementing this yourself, you can use image for loading and accessing images, or ndarray for a more general-purpose multidimensional array implementation.