Skip to content

Instantly share code, notes, and snippets.

@ihrwein
Last active January 5, 2016 19:49
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 ihrwein/a4558d63d9250ee0bbf6 to your computer and use it in GitHub Desktop.
Save ihrwein/a4558d63d9250ee0bbf6 to your computer and use it in GitHub Desktop.
getch() in Rust
/*
1. copy getch() into getch.c from here: http://stackoverflow.com/a/912796/3989982
2. compile getch.c with: gcc -o libgetch.so -shared -fPIC getch.c
3. create getch.rs with the below content
4. compile getch.rs: rustc -L. getch.rs
5. run it: LD_LIBRARY_PATH=. ./getch
You can extract these steps into a build script (or someone can make a platform independent getch based on this)
*/
#[link(name="getch")]
extern {
fn getch() -> u8;
}
fn main() {
println!("Type something!");
loop {
unsafe {
println!("{:?}", getch());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment