Skip to content

Instantly share code, notes, and snippets.

@matklad
Created May 2, 2020 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matklad/00f2a1b88407a770db7c97c83f1dbd35 to your computer and use it in GitHub Desktop.
Save matklad/00f2a1b88407a770db7c97c83f1dbd35 to your computer and use it in GitHub Desktop.
12:10:54|~/tmp
λ bat -p main.cpp
#include <iostream>
int square(int x) {
return x * x;
}
int main(void) {
int x = 1;
for (int i = 0; i < 10; ++i) {
if (square(x) < 0) {
std::cout << "square(" << x << ") < 0" << std::endl;
}
x *= 10;
}
}
12:10:58|~/tmp
λ clang++ main.cpp && ./a.out
square(1000000) < 0
square(1000000000) < 0
12:11:05|~/tmp
λ clang++ -O2 main.cpp && ./a.out
12:11:12|~/tmp
λ bat -p main.rs
fn square(x: i32) -> i32 {
x * x
}
fn main() {
let mut x: i32 = 1;
for _ in 0..10 {
if square(x) < 0 {
println!("square({}) < 0", x);
}
x *= 10;
}
}
12:11:27|~/tmp
λ rustc main.rs && ./main
thread 'main' panicked at 'attempt to multiply with overflow', main.rs:2:3
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
12:11:36|~/tmp
λ rustc -C opt-level=3 main.rs && ./main
square(1000000) < 0
square(1000000000) < 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment