Tuples and Function Pointers

Edit - fixed code link!

I'm having trouble calling a function from a tuple.

Here is the Code

I've included it all so maybe you guys and help me figure out what is going on. I'm just learning Rust and really liking it, but this compiles and runs without error, except it does nothing. I'm not getting any good warnings either.

if op.2.is_match(&format!("{:016b}", i)) { println!("Found Match! {:?}", op.0); op.1(i); }

If I comment out the op.1(i) then the match works and println prints Found Match! and op.0. Otherwise it prints nothing within the match.

You'll have to refer the the Gist to see how I set the function pointer. (It's short code I promise).
I'm sure I'm just doing something silly with the Tuple and function pointer. Any help is greatly appreciated and any tips of the design of code overall is also appreciated. The end goal of this program is to generate functions for a 68k CPU emulator. I do understand that I should break the program into modules, I'm just trying to get the best functionality working before I break it into modules.

1 Like

You may want to check that link again. It goes to some javascript file.

Edit: Oh, it works if the .js extension is removed.

I believe you should have got a warning:

warning: literal out of range for u16, #[warn(overflowing_literals)] on by default
  --> src/main.rs:56:19
56 |>     for i in 0x0..0x10000 {
   |>                   ^^^^^^^

Since it is essentially same to 0..0, the loop won't execute its body. (A quick way to solve this is to change 0x10000 to 0x10000u32 and fix other usages of i as required.)

Ah. Ok, the problem is type inference...

When you call op.1(i), rust learns that i must be a u16 so you iterate from 0 to 0. When you don't call op.1(i), rust assumes that i is an i32 (the default).

A quick way to solve this is to change 0x10000 to 0x10000u32 and fix other usages of i as required.

The correct way to fix the problem is to iterate over 0x0...0xFFFF. Unfortunately, inclusive ranges aren't stable. A stable way to fix this would be to iterate over (0u16..).take(0x10000)


Thanks very much! That fixed it.