rust - Is there a subtrait of `Index` that specifies the `len` method? -


the std::ops::index trait implemented types support array subscript notation. appears types implement index have len method, not part of trait can't assume exists. therefore, find myself writing code specialized slices (which have len method), prefer more general.

is there subtrait of index specifies len method or in other way reveals range of indices allowed?

is there subtrait of index specifies len method or in other way reveals range of indices allowed?

to best of knowledge, not in standard library.


i note, though, seem under misconception that:

  1. index returns result contiguous range of keys
  2. index contiguous range starts @ 0

both assumptions necessary len useful here (since suppose interested in checking before calling [] key in present).

so, in essence, asking hierarchy of traits:

  • index, allows querying element key
  • rangeindex, allows querying element key, , guarantees valid keys form contiguous range; rangeindex have range method returning range of valid keys
  • zerobasedrangeindex, allows querying element numeric keys, , guarantees valid form contiguous range starting 0; zerobasedrangeindex have len method returning number of valid keys

and of course have duplicated indexmut.

note: example, 1 implement index<k, output=v> btreemap<k, v>...


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -