Created
February 1, 2018 10:12
-
-
Save deanhu2/a8ff29daf2c36ab13b0c45e918eaea17 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern { | |
// Our C function definitions! | |
pub fn strcpy(dest: *mut u8, src: *const u8) -> *mut u8; | |
pub fn puts(s: *const u8) -> i32; | |
} | |
fn main() { | |
let x = b"Hello, world!\0"; // our string to copy | |
let mut y = [0u8; 32]; // declare some space on the stack to copy the string into | |
unsafe { | |
// calling C code is definitely unsafe. it could be doing ANYTHING | |
strcpy(y.as_mut_ptr(), x.as_ptr()); // we need to call .as_ptr() to get a pointer for C to use | |
puts(y.as_ptr()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment