Compiler Explorer does Rust. Rust really is strict: some Tweets that helped me learn how strict.
Posted by jpluimers on 2025/05/15
Just remembered that I had a fall 2021 note lying around about Compiler Explorer having evolved and been doing Rust for quite a while now: [Wayback/Archive] Compiler Explorer: G54Mb9Es3
gcc.godbolt.orgCompiler Explorer - Rust (rustc 1.55.0)pub fn add(a: u64, b: u16) { println!("{}", a + b);
It shows that integer types [Wayback/Archive] u16 and [Wayback/Archive] u64 cannot be added together without conversion or casting. Which is an example of the strictness that Rust requires. I think that is a good thing,
Via this tweet tree (as opinions on idioms vary so it is good to understand why):
- Thread starting at [Wayback/Archive] Ada Lovecraft 🐀 on Twitter: “so I’m actually sitting down and trying to learn some rust and uh..
rustc: error[E0277]: cannot add `u16` to `u64`I miss integer promotion already”- [Wayback/Archive] Ada Lovecraft 🐀 on Twitter: “computers: *are really good at adding numbers* rust: no”
- [Wayback/Archive] Ada Lovecraft 🐀 on Twitter: “question for the rustaceans in the crowd, which is more idiomatic?
a + (b as u64)a + u64::from(b)(both produce the same assembly in an optimized build)”- [Wayback/Archive] Manish on Twitter: “@lunasorcery
as, also as binds tighter than most operators so you don’t need parens usually (might still want em for clarity)” - [Wayback/Archive] amos (@fasterthanlime@octodon.social) on Twitter: “@ManishEarth @lunasorcery also “
x as _” works in a bunch of places so you don’t have to repeat yourself” - [Wayback/Archive] type class struggle tweets on Twitter: “@lunasorcery the first one”
- [Wayback/Archive] type class struggle tweets on Twitter: “@lunasorcery actually- I would say the first one is more common, but the second one is better, because if you change the type of
bto something that can’t be converted losslessly (u128,i16, etc.), it will produce an error rather than silently truncating”- [Wayback/Archive] Lesbian Deer 🦌 on Twitter: “@onfiv @lunasorcery You could also say `
a + b.into()`”- [Wayback/Archive] Iain on Twitter: “@Esper_Lily @onfiv @lunasorcery I’m still a rust newbie… which trait is that?”
- [Wayback/Archive] Lesbian Deer 🦌 on Twitter: “@onfiv @lunasorcery You could also say `
- [Wayback/Archive] type class struggle tweets on Twitter: “@lunasorcery actually- I would say the first one is more common, but the second one is better, because if you change the type of
- [Wayback/Archive] Manish on Twitter: “@lunasorcery
- [Wayback/Archive] Richard is on Mastodon @penllawen@infosec.exchange on Twitter: “@lunasorcery Huh, really? The type safety is that, uhh, safe?!”
- [Wayback/Archive] Ada Lovecraft 🐀 on Twitter: “@PenLlawen”
[Wayback/Archive] Compiler Explorer:
G54Mb9Es3
gcc.godbolt.orgCompiler Explorer - Rust (rustc 1.55.0)pub fn add(a: u64, b: u16) { println!("{}", a + b);
- [Wayback/Archive] Ada Lovecraft 🐀 on Twitter: “@PenLlawen”
And some more documentation links:
- [Wayback/Archive] Underscore – Rust Community Wiki
- [Wayback/Archive] Traits – Rust By Example
- [Wayback/Archive] Traits: Defining Shared Behavior – The Rust Programming Language
Note: Traits are similar to a feature often called interfaces in other languages, although with some differences.
- [Wayback/Archive] Advanced Traits – The Rust Programming Language
- [Wayback/Archive] Traits: Defining Shared Behavior – The Rust Programming Language
- [Wayback/Archive]
FromandInto– Rust By Example
Related 2017 blog post: Compiler Explorer – how various C++ compilers translate code into various machine code targets
--jeroen
https://twitter.com/lunasorcery/status/1449400981188382722
so I’m actually sitting down and trying to learn some rust and uh..
rustc: error[E0277]: cannot addu16tou64
I miss integer promotion already






Leave a comment