The trait bound is not satisfied in Rust

12,024

The reason is given to you in the notes:

note: slice indices are of type `usize`
slice indices are of type `usize` or ranges of `usize`

You need to cast the i32 value to usize, for example:

table[(field - 1) as usize]

Alternatively, consider using usize as the type of the field variable, if it makes sense in your application.

Share:
12,024
Nikola Stojaković
Author by

Nikola Stojaković

Software developer (mostly for web), animal and music lover.

Updated on August 20, 2022

Comments

  • Nikola Stojaković
    Nikola Stojaković over 1 year

    I'm trying to write a Tic Tac Toe game in Rust, but this function for changing a field doesn't work and I don't know what's wrong with it:

    fn change_field(mut table: [char; 9], field: i32, player: char) -> bool {
        if field > 0 && field < 10 {
            if table[field - 1] == ' ' {
                table[field - 1] = player;
                return true;
            } else {
                println!("That field isn't empty!");
            }
        } else {
            println!("That field doesn't exist!");
        }
    
        return false;
    }
    

    I'm getting these errors:

    src/main.rs:16:12: 16:26 error: the trait bound `[char]: std::ops::Index<i32>` is not satisfied [E0277]
    src/main.rs:16         if table[field-1] == ' ' {
                              ^~~~~~~~~~~~~~
    src/main.rs:16:12: 16:26 help: run `rustc --explain E0277` to see a detailed explanation
    src/main.rs:16:12: 16:26 note: slice indices are of type `usize`
    src/main.rs:17:13: 17:27 error: the trait bound `[char]: std::ops::Index<i32>` is not satisfied [E0277]
    src/main.rs:17             table[field-1] = player;
                               ^~~~~~~~~~~~~~
    src/main.rs:17:13: 17:27 help: run `rustc --explain E0277` to see a detailed explanation
    src/main.rs:17:13: 17:27 note: slice indices are of type `usize`
    src/main.rs:17:13: 17:27 error: the trait bound `[char]: std::ops::IndexMut<i32>` is not satisfied [E0277]
    src/main.rs:17             table[field-1] = player;
                               ^~~~~~~~~~~~~~
    

    In later versions of Rust, I get these errors:

    error[E0277]: the trait bound `i32: std::slice::SliceIndex<[char]>` is not satisfied
     --> src/main.rs:3:12
      |
    3 |         if table[field - 1] == ' ' {
      |            ^^^^^^^^^^^^^^^^ slice indices are of type `usize` or ranges of `usize`
      |
      = help: the trait `std::slice::SliceIndex<[char]>` is not implemented for `i32`
      = note: required because of the requirements on the impl of `std::ops::Index<i32>` for `[char]`
    
    error[E0277]: the trait bound `i32: std::slice::SliceIndex<[char]>` is not satisfied
     --> src/main.rs:4:13
      |
    4 |             table[field - 1] = player;
      |             ^^^^^^^^^^^^^^^^ slice indices are of type `usize` or ranges of `usize`
      |
      = help: the trait `std::slice::SliceIndex<[char]>` is not implemented for `i32`
      = note: required because of the requirements on the impl of `std::ops::Index<i32>` for `[char]`
    

    This is my first project in Rust so I don't have much experience with it. I tried to change the field to u32 too.