0

I was trying out Rust for the first time and was playing around with unsigned literals. When I run the following program

fn main() {
    println!("{x}", x = 1 - 2);
}

all is well and good, you get the expected output of -1.

When I changed the statement to println!("{x}", x = 1u32 - 2); and re-build with cargo build & cargo run, as expected, it would give the following : attempt to compute 1_u32 - 2_u32, which would overflow.

However if at this point I revert it back to the original statement and try to cargo build (works fine) & cargo run (fails) again, it gives thread 'main' panicked at 'attempt to subtract with overflow', src/main.rs:2:25. Why is it still assuming it's an unsigned integer? According to the docs it should infer it to be i32.

If at this point I try to change the statement to println!("{x}", x = 1i32 - 2);, I get the following error :

  = note: Undefined symbols for architecture x86_64:
            "core::fmt::ArgumentV1::new_display::h04b0a7b0eb5c5251", referenced from:
                playground::main::h42d87684ccf792ed in playground-5cdb2e37b25b0f71.272flcw1p2i5zhh8.rcgu.o
          ld: symbol(s) not found for architecture x86_64
          clang: error: linker command failed with exit code 1 (use -v to see invocation)

After this, any subsequent builds will give me the above error, whatever I try with the literals, unless I force it back to u32 explicitly.

Things work fine if I cargo clean and build again.

How is the incremental compiler meant to be used, should I do a cargo clean in between every build? Is this an issue with the incremental compiler / my OS or am I missing something fundamental?

I'm using Mac OS Ventura with the latest available xcode-tools & rustc 1.68.2.

One more observation : with different opt-levels, the output differs, and this is not reproducible on opt-level = 3

0 Answers0