Skip to content

Instantly share code, notes, and snippets.

@jfo
Created November 17, 2017 21:40
Show Gist options
  • Save jfo/798a7cb66ae782f770e682cc0d37c852 to your computer and use it in GitHub Desktop.
Save jfo/798a7cb66ae782f770e682cc0d37c852 to your computer and use it in GitHub Desktop.
const warn = @import("std").debug.warn;
fn dangle1() -> &u8 {
var x: u8 = 5;
&x
}
fn dangle2() -> &u8 {
var x: u8 = 5;
const y: &u8 = &x;
y
}
test "ohhh noooooo" {
// yassssssss ty
/// error: function returns address of local variable
_ = dangle1();
// nej tak
/// ohhhh noooooooo!
const shittypointer = dangle2();
warn("{}", *shittypointer); // ¯\_(ツ)_/¯
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment