0

I'm trying to make a very simple game, using Raylib bindings for Rust. I'm new to Rust, but I've been learning it for a couple of months and already made several simple projects that work as intended.

In this case I got to the point where I need to apply textures, and first I'm loading the file into an Image struct, so I can do some stuff with it before passing it to GPU.

After loading, I want to resize the image to the desired resolution, and this is when I get this error message.

Here's the relevant code (two variants):

fn get_image (filename: &str) -> Image {
    Image::load_image(filename)
        .expect("Couldn't load image from file")
        .resize(256, 256)
}

or

fn get_image (filename: &str) -> Image {
    let mut image = Image::load_image(filename)
        .expect("Couldn't load image from file");

    image.resize(256, 256)
}

In both cases I get the same error message:

mismatched types

instead of the `()` output of method `resize`

object_rend.rs(9, 34): expected `raylib::prelude::Image` because of return type

method `resize` modifies its receiver in-place 

The problem here is the method "resize", without it, just loading, the function works. But I really want to pack at least resizing into it, and in any case, I will need to resize this image later.

The method source code is here.

I understand what the problem is: this function/method doesn't return anything, but isn't that how all methods work, they just modify the existing value. So how do I fix this?

There's hardly any information online about Raylib for Rust specifically. As far as I get from the source code, it's all just the original Raylib C functions, wrapped in Unsafe blocks. I guess, one solution would be to pick another Rust engine, like ggez, but I really like how Raylib works.

Yuriy S
  • 103
  • 6
  • 1
    Just return image after resizing it: `let mut image = ...; image.resize(...); image` – cafce25 Dec 28 '22 at 23:07
  • @cafce25, haha, I feel stupid now, so this function is actually a procedure, that's why it doesn't return the value, and it leaves the variable in-scope. Now it works, thanks. – Yuriy S Dec 28 '22 at 23:08

0 Answers0