Reputation: 35
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
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:
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._
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
.Upvotes: 1
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