Chapter 07 - Damage get_available_exits() function errors

Open Beginner friendly
#140 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
1/5
Estimated time
Under an hour
Newbie friendliness
68/100
Issue type
Documentation
Clarity
Clearly specified
Activity status
Stale
Tech stack
rust
Domain
documentation

Research direction

Review the Chapter 07 - Damage tutorial and compare its get_available_exits example with the trait implementation at src/map.rs:117. Update the tutorial's Vec return and declaration to match the rltk::SmallVec type shown by the compiler, then verify that the Chapter 07 code builds and produces the expected monster behavior.

Written by the indexing model from the issue text.

Description

Hello,

I am working through the tutorial you have online for Rust and Rltk. I am on Chapter 07 - Damage and was hitting an error when you had us add

fn get_available_exits(&self, idx:usize) -> Vec<(usize, f32)> {
    let mut exits : Vec<(usize, f32)> = Vec::new();
    let x = idx as i32 % self.width;
    let y = idx as i32 / self.width;
    let w = self.width as usize;

    // Cardinal directions
    if self.is_exit_valid(x-1, y) { exits.push((idx-1, 1.0)) };
    if self.is_exit_valid(x+1, y) { exits.push((idx+1, 1.0)) };
    if self.is_exit_valid(x, y-1) { exits.push((idx-w, 1.0)) };
    if self.is_exit_valid(x, y+1) { exits.push((idx+w, 1.0)) };

    // Diagonals
    if self.is_exit_valid(x-1, y-1) { exits.push(((idx-w)-1, 1.45)); }
    if self.is_exit_valid(x+1, y-1) { exits.push(((idx-w)+1, 1.45)); }
    if self.is_exit_valid(x-1, y+1) { exits.push(((idx+w)-1, 1.45)); }
    if self.is_exit_valid(x+1, y+1) { exits.push(((idx+w)+1, 1.45)); }

    exits
}

To the BaseMap impl. The error I was receiving was:

error[E0053]: method `get_available_exits` has an incompatible type for trait
   --> src/map.rs:117:5
    |
117 |     fn get_available_exits(&self, idx:usize) -> Vec<(usize, f32)> {
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `smallvec::SmallVec`, found struct `std::vec::Vec`
    |
    = note: expected fn pointer `fn(&map::Map, _) -> smallvec::SmallVec<[(usize, f32); 10]>`
               found fn pointer `fn(&map::Map, _) -> std::vec::Vec<(usize, f32)>`

After looking over the source code you provide I found that instead of using Vec like you have in the tutorial you used rltk::SmallVec. After changing my code to use rltk::SmallVec the error went away and I was getting the expected monster behavior.

Just wanted to bring this up in case others have hit this issue.

Dominant language
Rust
Stars
968
Forks
166
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from amethyst/rustrogueliketutorial

All issues in amethyst/rustrogueliketutorial

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.