Created
December 21, 2018 15:49
-
-
Save megumish/1047fe71c39db36b75101a82161fe87b to your computer and use it in GitHub Desktop.
memcpy in rust
This file contains 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
[package] | |
name = "memcpy" | |
version = "0.1.0" | |
authors = ["Keishi Kawada <megumish@exploitouri.st>"] | |
edition = "2018" | |
[dependencies] | |
failure = "0.1.3" |
This file contains 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
fn memcpy(input: &[u8], output: &mut [u8], size: usize) -> Result<(), failure::Error> { | |
if output.len() > size { | |
return Err(failure::err_msg("Too Big")); | |
} | |
let mut i = 0; | |
for x in input[0..size].iter() { | |
output[i] = *x; | |
i += 1; | |
} | |
Ok(()) | |
} | |
fn main() -> Result<(), failure::Error>{ | |
let u = [1; 10]; | |
let mut v = [0; 10]; | |
memcpy(&u, &mut v, 10)?; | |
println!("{:?}", v); | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment