Using Variable Bindings as Array Indices in Rust

Today marks the first day that I’ve begun my journey with Rust 1.0. So far, I’m loving it. However, there are some parts that can be very confusing.

While trying to make a simple Tic-Tac-Toe game, I came across a problem where I could not find the correct type for array indices. I wasn’t sure where to look in the documentation, but I ended up finally finding the solution. If you were wondering, array indices must be of type usize. Here is the code example I was using for reference,

let mut answer = String::new();                                             
io::stdin().read_line(&mut answer)                                          
     .ok()                                                                   
    .expect("Error reading line");                                          
                                                                             
let answer:usize = match answer.trim().parse() {                            
     Ok(num) => num,                                                         
     Err(_) => 0,                                                            
 };                                                                          
                                                                               
 board = 'X';

One thought on “Using Variable Bindings as Array Indices in Rust

  1. Pingback: Building Tic-Tac-Toe in Rust: rustic_tac_toe | brandonio21

Leave a Reply

Your email address will not be published. Required fields are marked *