-1

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)

  • 1
    I've never used this library but I'm going to guess that `Drawable` is implemented for `Mesh`, instead of `Result`. Try handling the error either with the `?` operator, `.unwrap()` if you know it will always succeed, or by `match`ing on it. I'd recommend the Rust Book's chapter on error handling – cameron1024 Mar 01 '22 at 14:09
  • 1
    It's hard to answer your question because it doesn't include a [MRE]. We can't tell what crates (and their versions), types, traits, fields, etc. are present in the code. It would make it easier for us to help you if you try to reproduce your error on the [Rust Playground](https://play.rust-lang.org) if possible, otherwise in a brand new Cargo project, then [edit] your question to include the additional info. There are [Rust-specific MRE tips](//stackoverflow.com/tags/rust/info) you can use to reduce your original code for posting here. Thanks! – Shepmaster Mar 01 '22 at 14:09
  • 1
    Your question might be answered by the answers of [Unable to read file contents to string - Result does not implement any method in scope named `read_to_string`](https://stackoverflow.com/q/29214963/155423). If not, please **[edit]** your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Mar 01 '22 at 14:11
  • 1
    It's great that you have the solution to your question! You should post it as an answer rather than an edit to your question and then potentially accept that answer. That way, the question shows up as solved in search results, people can vote on your answer, and your solution can be more helpful to future people with the same problem. – Shepmaster Mar 01 '22 at 14:23

2 Answers2

0

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.

Solomon Ucko
  • 5,724
  • 3
  • 24
  • 45
0

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)