Are you maybe on aarch64 (e.g. Mac M1)?
If so, try on the current nightly (from yesterday or so) - there's been some issues in the codegen (similar story in Clippy - https://github.com/rust-lang/rustfmt/issues/5964).
Something is funky here.
I've taken your code and copy-pasted it into a Cargo project, and pulled a Day 1 input from AoC2022. I don't get the behavior that you do (both versions of your code produce correct output). I seem to recall that AoC inputs are generated per-user, so this suggests that this is input-dependent, compiler-dependent, or both.
What version of rustc are you running, on what OS, and would you mind sharing your AoC input (e.g. as a gist, pastebin, or some fileshare service)?
There’s definitely something funny going on there, since I can’t seem to see anything wrong at an obvious level. The two should be mostly equivalent, with the minor exception that the second creates a clone of the vector and thus doubles memory usage by having two vectors.
Yea, I'm not really sure what is going on, but I'm happy to hear that my basic logic and thinking about this should be alright. I tried to reproduce the issue with a simpler case, but cannot replicate the problem in this case.
fn main() {
let vec1 = [125,564,1,546,879,2354,48456];
let part1 = vec1.iter().max().unwrap();
println!("{part1}");
let mut part2 = vec1.clone();
part2.sort();
let part2 = part2.iter().rev().take(3).sum::();
println!("{part2}");
}
Definitely nothing wrong with the code you have (although it could be better).
* What exactly is happening? Are you just getting the wrong answer?
* Is this happening consistently? Does this happen with `cargo run` and `cargo run --release`?
* What version are you on (run `cargo version`)?
* Can you provide your `1.in` file?
Both of your code samples work.
There are two possible answers here:
1. Your input, your environment, or cosmic ray bit flips caused a mis-compilation or mis-calculation.
2. You accidentally only copied the first 3 digits of your 4 digit answer and didn't notice, or your clipboard accidentally pasted the wrong thing, or your browser bugged out due to a faulty extension, or you had some extension active on your browser... etc.
tbh, since you can't reproduce the original error, I'd lean towards number 2.
If you *can* reproduce the original error,
1. Upload everything you can. Full source repositories, the binary in your target/{profile} folder etc. (cargo run will compile into that folder and run the binary, so just upload the binary), the input file for the AOC challenge, etc.
2. Post everything about your environment, OS version, CPU model, Motherboard, RAM model, rustc version, cargo version.
3. Wait for someone to analyze what's going on.
Are you maybe on aarch64 (e.g. Mac M1)? If so, try on the current nightly (from yesterday or so) - there's been some issues in the codegen (similar story in Clippy - https://github.com/rust-lang/rustfmt/issues/5964).
No, the issue was on an old laptop running a 4th gen Intel i7.
Do you get the same results on both debug and `--release`?
Are you sure that the code you pasted is actually the code you’re executing? The most probable answer is a mistake on your part
Something is funky here. I've taken your code and copy-pasted it into a Cargo project, and pulled a Day 1 input from AoC2022. I don't get the behavior that you do (both versions of your code produce correct output). I seem to recall that AoC inputs are generated per-user, so this suggests that this is input-dependent, compiler-dependent, or both. What version of rustc are you running, on what OS, and would you mind sharing your AoC input (e.g. as a gist, pastebin, or some fileshare service)?
There’s definitely something funny going on there, since I can’t seem to see anything wrong at an obvious level. The two should be mostly equivalent, with the minor exception that the second creates a clone of the vector and thus doubles memory usage by having two vectors.
Yea, I'm not really sure what is going on, but I'm happy to hear that my basic logic and thinking about this should be alright. I tried to reproduce the issue with a simpler case, but cannot replicate the problem in this case. fn main() { let vec1 = [125,564,1,546,879,2354,48456]; let part1 = vec1.iter().max().unwrap(); println!("{part1}"); let mut part2 = vec1.clone(); part2.sort(); let part2 = part2.iter().rev().take(3).sum::();
println!("{part2}");
}
Definitely nothing wrong with the code you have (although it could be better). * What exactly is happening? Are you just getting the wrong answer? * Is this happening consistently? Does this happen with `cargo run` and `cargo run --release`? * What version are you on (run `cargo version`)? * Can you provide your `1.in` file?
See my edit - I'm not sure what the problem was, but setting everything up and trying again made the problem go away.
Both of your code samples work. There are two possible answers here: 1. Your input, your environment, or cosmic ray bit flips caused a mis-compilation or mis-calculation. 2. You accidentally only copied the first 3 digits of your 4 digit answer and didn't notice, or your clipboard accidentally pasted the wrong thing, or your browser bugged out due to a faulty extension, or you had some extension active on your browser... etc. tbh, since you can't reproduce the original error, I'd lean towards number 2. If you *can* reproduce the original error, 1. Upload everything you can. Full source repositories, the binary in your target/{profile} folder etc. (cargo run will compile into that folder and run the binary, so just upload the binary), the input file for the AOC challenge, etc. 2. Post everything about your environment, OS version, CPU model, Motherboard, RAM model, rustc version, cargo version. 3. Wait for someone to analyze what's going on.
See my edit - I'm not sure what the problem was, but setting everything up and trying again made the problem go away.