Skip to content

Instantly share code, notes, and snippets.

@jbenner-radham
Created December 10, 2013 01:53
Show Gist options
  • Save jbenner-radham/7884597 to your computer and use it in GitHub Desktop.
Save jbenner-radham/7884597 to your computer and use it in GitHub Desktop.
Playing around with file system navigation in Rust.
// Tested on Rust 0.8 on OS X.
fn main() {
use std::*;
// This include is for the POSIX dirent style methods like "is_dir()".
use std::rt::io::file::*;
// Returns a "Path" struct (kind of like the var name, OMG!)
let path = os::getcwd();
/* -----------------------------------------------------
* The following two methods are functionally identical.
* -----------------------------------------------------
*
* let files = os::list_dir_path(&path);
*
* for file in files.iter() {
* if file.is_dir() {
* // Returns a "PosixPath" struct.
* println!("{:?}", file.to_str());
* }
* }
*
* -----------------------------------------------------
*
* let files = os::list_dir(&path);
*
* for file in files.iter() {
* if Path(*file).is_dir() {
* // Returns a borrowed "&" string pointer.
* println!("{:?}", *file);
* }
* }
*
* -----------------------------------------------------
*/
// -------------------------------------------------------------------
// A pointer "*" dereferences memory which is:
// -------------------------------------------------------------------
// "~" | Owned - Allocated on the heap memory.
// "@" | Managed - Memory is managed by the Rust garbage collector.
// "&" | Borrowed - A reference pointer to memory allocated elsewhere.
// -------------------------------------------------------------------
let files = os::list_dir(&path);
for file in files.iter() {
if Path(*file).is_dir() {
println!("{:?}", file);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment