Hacker News new | ask | show | jobs
by tialaramex 1292 days ago
Note that for AoC, it will often be a good idea to say you want bytes, not chars, and of course a slice of bytes is just trivially indexable. You can make "byte string literals" and "byte literals" very easily in Rust, just with a b-prefix and the obvious restriction that only ASCII works since the multi-byte characters are not single bytes. The type of a "byte literal" is u8, a byte, and the type of a "byte string literal" is &'static [u8; N] a reference to an array of bytes which lives forever.

  let s1 = "[[..]]";
  // Rats, indexing into s1 doesn't work †

  let s2 = b"[[..]]";
  // s2 is just an array of bytes
  assert_eq!(s2[4], b']');
† Technically it works fine, it's just probably not what you wanted