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
specifieslen
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:
index
returns result contiguous range of keysindex
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 keyrangeindex
, allows querying element key, , guarantees valid keys form contiguous range;rangeindex
haverange
method returningrange
of valid keyszerobasedrangeindex
, allows querying element numeric keys, , guarantees valid form contiguous range starting0
;zerobasedrangeindex
havelen
method returning number of valid keys
and of course have duplicated indexmut
.
note: example, 1 implement index<k, output=v>
btreemap<k, v>
...
Comments
Post a Comment