pub fn print_result(bcd: &BinaryCalculationData)
{
let o1 = match bcd.operand_1
{
Some(v) => v,
None => "None"
};
let o2 = match bcd.operand_2
{
Some(v) => v,
None => "None"
};
let answer = match bcd.answer
{
Some(v) => v,
None => "None"
};
println!("{} {} {} = {}", o1, bcd.operation, o2, answer);
}
No dice because v is a i32 and I’m trying to potentially assign &strNone as you can see. You can see what I’m trying to do though… I want to “unwrap” these Options so I can print them.
You’d actually want to use unwrap_or_else(|| "None".to_owned()) in this situation because unwrap_or will eagerly evaluate the string-allocating code, whereas unwrap_or_else won’t.
I’m glad that you identified this because I had the impression that I should avoid working with Ok(()) for semantic reasons but actually, this allows us to display errors in the opposite case using ?.
For anyone else who reads this, I also just stumbled upon this article which is obviously a couple years old, but still a bit helpful.