1

I am working through some of the Exercism exercises. I am running into an issue when I try to use variables as the bounds for a range. The code I am working on is as follows:

pub fn test(start: i32, end: i32) {
    println!("{}-{}", start, end);
    for n in start..end {
        println!("IN LOOP: {}", n);
    }
}

fn main() {
    test(7, 5);
}

The issue happens when I try to call this function with any set of start and end params. I can see the output from the first println! call, but the "IN LOOP" print never runs. If I replace this line for n in (start..end) with something like for n in (3..0) it runs successfully. I am very curious why it seems like I cannot create a range bounded by variables.

The example is available on The rust playground

Peter Hall
  • 53,120
  • 14
  • 139
  • 204
George Griffin
  • 634
  • 7
  • 17
  • Please review how to create a [MCVE] and then [edit] your question to include it. We cannot tell what crates, types, traits, fields, etc. are present in the code. Namely, `verse` is missing. Please remove it if it's irrelevant or provide it if it's needed. Try to produce something that reproduces your error on the [Rust Playground](https://play.rust-lang.org) or you can reproduce it in a brand new Cargo project. There are [Rust-specific MCVE tips](//stackoverflow.com/tags/rust/info) as well. – Shepmaster Dec 19 '18 at 00:21
  • Taking a guess at what `verse` might be, I see `IN LOOP` printed, so be sure to provide how you are calling the function, too. – Shepmaster Dec 19 '18 at 00:23
  • 1
    Taking a wild shot in the dark, I believe your question may answered by the answers of [How can I iterate over a backwards range?](https://stackoverflow.com/q/42545548/155423). If you disagree, please [edit] your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Dec 19 '18 at 00:25
  • 1
    Maybe not even a shot in the dark, seeing as how the duplicate uses the exact same function names and arguments as you do. – Shepmaster Dec 19 '18 at 00:28
  • 1
    That question actually does answer my question. I created a minimal example that demonstrates the problem. Should I update my question to reflect that, or should we just mark as already answered? – George Griffin Dec 19 '18 at 00:29
  • 1
    Both are good. The MCVE will help other people who find this question via search engines to tell if it matches their case and the duplicate will answer it. – Shepmaster Dec 19 '18 at 00:33

0 Answers0