Journey with Rust Part 5: Pure Evil (with Milk and Sugar)

Software engineers drink coffee.
Tom drinks coffee.
Is Tom a software engineer?

Getting this question during yet another mind-scan job interview (no toasters allowed) made me think about hot cappuccino. The absence of it, to be precise. Unfortunately, the only coffee machine I could find turned out to be pure evil disguised as a home appliance. Bright fuchsia-coloured display included.

use std::cell::Cell;
use rand;
// Parameters: [milk, sugar] — this is evil
fn make_coffee(mut coffee_params: [i32; 2]) -> i32 {
// This is even more evil
let evil_gnome = Cell::new(0);
// The evil gnome eats 5g of sugar —
// an offering for beverage preparation
coffee_params[evil_gnome.get()] -= {
evil_gnome.set(1); // ! Deepest level of hell kind of evil !
5
};
let milk = coffee_params[0];
let sugar = coffee_params[1];
// Compared to the rest, the fact that you might
// get no coffee in your coffee is not evil at all
let coffee = rand::random_range(0..5);
println!("Here is your coffee. Milk: {milk} Sugar: {sugar}");
milk + sugar + coffee
}
fn main() {
const MILK_I_WANT: i32 = 20;
const SUGAR_I_WANT_PLUS_5_FOR_GNOME: i32 = 10;
println!("{:?}", make_coffee([
MILK_I_WANT,
SUGAR_I_WANT_PLUS_5_FOR_GNOME
]));
}

It works. The array access logic makes my brain flip a few times — but it works. One might even get a decent cup of coffee if the gods of randomness are in a good mood.

Here is your coffee. Milk: 20 Sugar: 5

I got my coffee. The gnome got his sugar. And since I’m a software engineer, I simply couldn’t leave this ugly code as it was. Refactoring! Do your thing!

use std::cell::Cell;
use std::ops::{Add, SubAssign};
// Now its generic evil - the rest remains untouched
fn make_coffee<T>(mut coffee_params: [T; 2]) -> T
where
T: Copy + From<i32> + SubAssign +
Add<Output = T> + std::fmt::Display,
{
let evil_gnome = Cell::new(0);
coffee_params[evil_gnome.get()] -= {
evil_gnome.set(1); // ! No changes - still evil !
T::from(5)
};
let milk = coffee_params[0];
let sugar = coffee_params[1];
let coffee = T::from(rand::random_range(0..5));
println!("Here is your coffee. Milk: {milk} Sugar: {sugar}");
milk + sugar + coffee
}
fn main() {
const MILK_I_WANT: i32 = 20;
const SUGAR_I_WANT_PLUS_5_FOR_GNOME: i32 = 10;
println!("{:?}", make_coffee([
MILK_I_WANT,
SUGAR_I_WANT_PLUS_5_FOR_GNOME
]));
}

There – so much better. Now I can make coffee for Paul, who is very picky about the amount of sugar in his beverage. Exactly 6.13 grams, with double-precision accuracy.

Only now my old coffee tastes like distilled camel piss with too much sugar in it.

I quickly checked the log, leaving you no time to ask, “How do you know?” question. It confirmed what my tongue already suspected:

Here is your coffee. Milk: 15 Sugar: 10

What on Earth is this? Who drank my milk? OK, I know who. But why? WHY?

The intended logic did not change. The input did not change. Nothing really changed — even the generic type ends up being the very same i32 as in the first version.

And yet, the evil gnome drank milk instead of eating sugar.

From the Rust documentation:

Surprising indeed, it is. The order goes like this:

  • Both operand types known to be primitive before monomorphization (1st example) — right to left.
  • Generic types (2nd example) — left to right.

Common knowledge. Luckily (hopefully), not something one would ever encounter in production code.

And if you want to see a magic trick, just change:

coffee_params[evil_gnome.get()] -= {

to:

coffee_params[evil_gnome.get()] = {

No more compound assignment. No more surprises.

Now, back to the important stuff. Is Tom, or is Tom not, a software engineer? I have no clue, but he might quit drinking coffee after reading this post.

The journey continues…

Leave a Reply