Creating a custom ranged integer type

In some cases where an implementation should not have bad side effects, I think a panic is more "succinct" (although for every other case Result is really useful and the thing to do). TBH I'm not sure if the case I described qualifies for such an assertion, i.e index access should not have bad side effects.

Return a Result from where? A custom function (trait?) on the vector?

Maybe I can explain my usecase a bit better:

  • There's a fxn called with a list = eg A(mylist: Vec<myitems>)
  • In A() I initialize an "index" (mylist_index: usize) which should be constrained to the set of values defined by length of mylist.
  • A() calls another function passing it mylist & the index B(mylist, mylist_index).
  • B() can modify the index and access items in mylist. I was thinking if there's a way to avoid splattering index validity checks whenever manipulating the index value or using it to access mylist.