Why I can not use u8 as an index value of a Rust array?

后端 未结 1 1555
悲&欢浪女
悲&欢浪女 2020-12-20 20:27

I am new to Rust, and I am trying to write simple bitwise replacer.

I have this code:

const TABLE: [u64; 8] = [
    0xC462A5B9E8D703F1,
    0x68239A5         


        
相关标签:
1条回答
  • 2020-12-20 20:49

    You can look at the documentation for SliceIndex by searching the Rust standard library. The list of implementations of this trait at the bottom of the documentation page indicates that this trait is implemented for usize and various usize ranges.

    This should answer both of your questions: indexing is not implemented for u8 type and you need to cast u8 to usize.

    (get_part(TABLE[left as usize], left) << 4) + get_part(TABLE[right as usize], right)
    
    0 讨论(0)
提交回复
热议问题