3 ms·
It looks like you can't. You can create a Range out of it, but the range is not an iterator. If you look at the Range docs[0], you'll see that it only implement
by bilkow 6y ago
It looks like you can't. You can create a Range out of it, but the range is not an iterator. If you look at the Range docs[0], you'll see that it only implements Iterator where its generic type (A) implements std::iter::Step.
Going to the Step docs[1] you'll see that its only implemented by integer types and char. If Vec implemented Step, you could iterate over it.
Also, if you're referring to the "contains" guessing puzzle, you have to look at Range::contains docs to see it uses Vec's PartialOrd implementation (basically comparison), in which docs state its elements are compared lexicographically.
Usually in Rust the first thing I'll do when trying to do something very specific (iterate over a Range of Vecs) is try it at the playground (play.rust-lang.org) and/or just look at std docs.
[0] https://doc.rust-lang.org/std/ops/struct.Range.html#impl-Iterator https://doc.rust-lang.org/std/ops/struct.Range.html#impl-Ite...
[1] https://doc.rust-lang.org/std/iter/trait.Step.html#implementors https://doc.rust-lang.org/std/iter/trait.Step.html#implement...
- andy_ppp 6y agoThat’s what I did (at repl.it anyway)... how do I print the values in a Range of Vectors?
- cdirkx 6y agoWhat would you expect those values to be?
- kibwen 6y agoThe range doesn't have values. A range defined by Rust has a start point and an end point, and no other semantics. Some additional functions then add behavior if the types contained in the range can be compared via greater-than, etc. Vecs in Rust can be compared if their contents can be compared. For a range of two vecs, foo and bar, to ask if a third vec is "contained" in that range is to ask if it compares greater than foo and less than bar.
- andy_ppp 6y agoOkay thanks for the explanation!
- redrobein 6y agoDoes debug print not work {:?}
- gpm 6y agoYou don't, because that doesn't make mathematical sense. Let's say I have the range [1].. [2], what is in this range? Well vectors are sorted in lexicographic order, so every vector that starts with 1. What comes after [1]? Suppose it's any vector whose first non zero digit is in place n (e.g. n = 2 and the vector is [1, 0, 3, 1]). I can make a vector that comes earlier in the ordering by just inserting another 0 (e.g. [1, 0, 0, 3, 1]). This means that the vector has no non zero digit... but it is obvious that [1] doesn't come after [1], and that [1, 1] comes before [2] so [2] isn't the next vector, so (by proof by contradiction) there actually just isn't a well defined concept of next vector. As a result I definitely can't just list the vectors in a range in order.
- nybble41 6y ago> there actually just isn't a well defined concept of next vector I think there is a well-defined next vector, it just isn't very useful. The next vector after [1] for a vector of unsigned integers would have to be [1, 0]. And then [1, 0, 0], [1, 0, 0, 0], etc. (For signed integers substitute T::MIN instead of 0.)