the trait `Drawable` is not implemented for `Result<Mesh, GameError>`

219 Views Asked by At

I am using the ggez library to create a simple pong game.

My code on the line of error:

let racket = graphics::Rect::new(10.0, 10.0, 300.0, 150.0);
let racket_mesh = graphics::Mesh::new_rectangle(ctx, graphics::DrawMode::fill(), racket, graphics::WHITE);
graphics::draw(ctx, &racket_mesh, graphics::DrawParam::default());

the error:

28  |         graphics::draw(ctx, &racket_mesh, graphics::DrawParam::default());
    |         ^^^^^^^^^^^^^^ the trait `Drawable` is not implemented for `Result<Mesh, GameError>`

Edit: it worked after adding ? at the end of the line like:


let racket_mesh = graphics::Mesh::new_rectangle(ctx, graphics::DrawMode::fill(), racket, graphics::WHITE,)?;

also adding a comma after the last element (weird coming from different languages)

2

There are 2 best solutions below

0
On BEST ANSWER

it worked after adding ? at the end of the line like:

let racket_mesh = graphics::Mesh::new_rectangle(ctx, graphics::DrawMode::fill(), racket, graphics::WHITE,)?;

also adding a comma after the last element (weird coming from different languages)

0
On

ggez::graphics::Mesh::new_rectangle returns GameResult<Mesh>, which an alias of Result<T, GameError>.

See https://stackoverflow.com/a/71270401/5445670:

You also need to use match, std::result::Result::unwrap, std::result::Result::expect, ?, or something else to handle the Err variant of the Result and access the value inside the Ok variant.