Skip to content

Instantly share code, notes, and snippets.

@deanhu2
Created February 1, 2018 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 deanhu2/a8ff29daf2c36ab13b0c45e918eaea17 to your computer and use it in GitHub Desktop.
Save deanhu2/a8ff29daf2c36ab13b0c45e918eaea17 to your computer and use it in GitHub Desktop.
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