r/learnrust • u/AdvanceAdvance • Jan 11 '24
Why does Rust produce inconsistent results?
So, for the first time in a decade of programming in various languages, I'm getting intermittently wrong results from execution. Specifically, I run the same non-network program with the same input and get different results.
I plan to hunt this down, paring the example down, etc. Really, I'm trying to understand why Rust allows this? I thought consistency was part of it's zen.
The core data structures are a cyclic tree of nodes, with each node having a three character identifier, and identifiers for the left and right:
type NodeList = HashMap<String, (String, String)>Also, I have a mutating vector of positions for simultaneously walking the nodes. That is, for each item in the vector, change it's string pointer to point to the corresponding node table's left or right entry. The suspicious code for this:
let mut positions : Vec<&str> = ....
...
for pos in &mut positions {let p = next_position(pos, lr, &nodes); *pos = p; }
When I run with the same data, I get different results.
Why?
Why does Rust allow it to compile?
The full code
https://gist.github.com/merriam/d5f63bc6cb279812396006ca1010fe6a
The output
General output with lots of debug information:
One version ends with
After 2 steps, next char is L, positions are ["11Z", "22C"]
After 4 steps, next char is L, positions are ["11Z", "22B"]
Final positions are ["11Z", "22Z"]
Ran for after 6 steps.
or sometimes
After 3 steps, next char is R, positions are ["22Z", "11B"]
Final positions are ["22Z", "11Z"]
Ran for after 6 steps.
The input
https://gist.github.com/merriam/23a4ce77889436d09966423b3429f6d5
LR
11A = (11B, XXX)11B = (XXX, 11Z) 11Z = (11B, XXX) 22A = (22B, XXX) 22B = (22C, 22C) 22C = (22Z, 22Z) 22Z = (22B, 22B) XXX = (XXX, XXX)

