sambia39
sambia39

Reputation: 35

how to correct the error "wrong number of type arguments: expected 1, found 0" on sdl2::render::Canvas

I am testing SDL2 under rust and I have written a function that is supposed to load an image how can I load my image and not have the following error "wrong number of type arguments: expected 1, found 0 " on the second parameter of my function?

 fn load_image(dtLoad: &str, canva: sdl2::render::Canvas) -> sdl2::render::Canvas {

    let _img_load = {
        let _x = match canva.texture_creator() {
            Ok(_x) => _x, 
            Err(_) => panic!("Error create canva"),
        };
        let _load = match _x.load_texture(dtLoad) {
            Ok(_load) => _load,
            Err(_) => panic!("Error load texture\t:{}", dtLoad),
        };
        canva.copy(&_load, None, None);
    };
    return _img_load;
}

I expect the function to load images with SDL Image into an SDL_Surface and return them to the caller. Except I get this error message:

error[E0107]: wrong number of type arguments: expected 1, found 0
 --> src/main.rs:4:60
  |
4 | fn load_image(dtLoad: &str, canva: sdl2::render::Canvas) -> sdl2::render::Canvas {
  |                                                             ^^^^^^^^^^^^^^^^^^^^ expected 1 type argument

Upvotes: 0

Views: 1413

Answers (2)

Lukas Kalbertodt
Lukas Kalbertodt

Reputation: 88626

sdl2::render::Canvas is defined as (as one can see in the documentation I linked):

struct Canvas<T: RenderTarget> { /* fields omitted */ }

This means it is generic over a T. As such you cannot use sdl2::render::Canvas just like that as it isn't a full type yet. You have to supply a generic parameter like sdl2::render::Canvas<WindowContext>. Without more context we cannot help you more than that.


Apart from that, some general hints:

  • Please use standard Rust formatting, including spacing and line breaks. You can use rustfmt to easily format your code. At the very least, format your code with rustfmt before asking on StackOverflow. You can use the Playground to format your code with rustfmt. I edited your question to fix this.
  • It is a very bad idea to start all variable names with _ as this disables unused variable warnings. Those warnings have a purpose. And even if you don't like those warnings, rather use #![allow(unused_variable)] instead of adding an underscore to each variable name. To be clear: I still do not recommend ignoring these warnings!
  • return should be omitted in Rust when possible. In your case it's the last statement so just write img_load.
  • Please at least try to make your StackOverflow question readable by formatting your code and error message. If you don't care, you can't expect others to care.

Upvotes: 1

ustulation
ustulation

Reputation: 3760

The context is not very clear here, but from what is given it seems Canvas is a generic type and thus should be supplied a type on which it's going to be specialised (implicit template specialisation).

So Canvas probably is similar to: struct Canvas<T> { .. }

To use it either your function needs to be a template or Canvas needs to take a type parameter for it's template:

fn load_image<T>(... Canvas<T>)-> Canvas<T> where T: ...{

or

fn load_image(... Canvas<SomeSpecificType>)-> Canvas<SomeSpecificType> {

The call site (probably in main from the {unformatted and difficult to follow - please use some formatting} error message) then should instantiate Canvas accordingly:

let c = Canvas<SomeSpecificType> {  ... };

Upvotes: 1

Related Questions