This confused me, a rust neophyte, so I wanted to ask for
clarification, inline below:

I apologize this is a little off topic; is there a better venue for
language-as-it-is learning versus design / implementation discussions?
 Has rust passed the threshold where those two topic groups split off
or are they still intertwingled?


On Mon, Oct 22, 2012 at 12:49 PM, Viktor Dahl <pazaconyo...@gmail.com> wrote:
> I believe that the addition of warnings for useless comparisons,
> similar to gcc's
> -Wtype-limits would be a nice feature to have for a safety-minded language
> such as Rust.
>
> Consider this program:
>
> fn main() {
>         let x = ~[1, 2, 3, 4, 5];
>         let mut i = x.len() - 1;
>         while i >= 0 {
>                 if x[i] <= 3 {
>                         io::println("At most three");
>                 }
>                 i -= 1;
>         }
> }
>
> The behaviour one might expect here is for the program to print "at most 
> three"
> three times and then exiting. The actual program output is:
>
> At most three
> At most three
> At most three
> rust: task failed at 'index out of bounds: the len is 5 but the index
> is -1', uint.rs:6

This is where I'm confused.  i should be inferred to be uint, so it
can never represent a negative value.

Does this mean the indexing operation takes a signed type and the
expression i - 1 is implicitly coerced?

Is the indexing operation a primitive language implementation, or does
it translate into a trait method or some other user-definable
behavior?  In the latter case, what is it's interface?  (Nothing in
the vec docs for 0.4 obviously implement v[i].)


> rust: domain main @0xdd9c10 root task failed
>
> The problem is that i is unsigned, so the comparison i >= 0 will always be
> true. With the addition of "-Wtype-limits" this (and similar errors) could
> easily be avoided.
> _______________________________________________
> Rust-dev mailing list
> Rust-dev@mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev


Nathan
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to