Skip to content

Add example to std::os documentation. #14641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 6, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 57 additions & 4 deletions src/libstd/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,24 @@ pub fn close(fd: int) -> int {
pub static TMPBUF_SZ : uint = 1000u;
static BUF_BYTES : uint = 2048u;

/// Returns the current working directory.
/// Returns the current working directory as a Path.
///
/// # Failure
///
/// Fails if the current working directory value is invalid:
/// Possibles cases:
///
/// * Current directory does not exist.
/// * There are insufficient permissions to access the current directory.
///
/// # Example
///
/// ```rust
/// // We assume that we are in a valid directory like "/home".
/// let current_working_directory = std::os::getcwd();
/// println!("The current directory is {}", current_working_directory.display());
/// // /home
/// ```
#[cfg(unix)]
pub fn getcwd() -> Path {
use c_str::CString;
Expand All @@ -80,7 +97,24 @@ pub fn getcwd() -> Path {
}
}

/// Returns the current working directory.
/// Returns the current working directory as a Path.
///
/// # Failure
///
/// Fails if the current working directory value is invalid.
/// Possibles cases:
///
/// * Current directory does not exist.
/// * There are insufficient permissions to access the current directory.
///
/// # Example
///
/// ```rust
/// // We assume that we are in a valid directory like "C:\\Windows".
/// let current_working_directory = std::os::getcwd();
/// println!("The current directory is {}", current_working_directory.display());
/// // C:\\Windows
/// ```
#[cfg(windows)]
pub fn getcwd() -> Path {
use libc::DWORD;
Expand Down Expand Up @@ -172,11 +206,20 @@ fn with_env_lock<T>(f: || -> T) -> T {
}
}

/// Returns a vector of (variable, value) pairs for all the environment
/// variables of the current process.
/// Returns a vector of (variable, value) pairs as a Vec<(String, String)>,
/// for all the environment variables of the current process.
///
/// Invalid UTF-8 bytes are replaced with \uFFFD. See `str::from_utf8_lossy()`
/// for details.
///
/// # Example
///
/// ```rust
/// // We will iterate through the references to the element returned by std::os::env();
/// for &(ref key, ref value) in std::os::env().iter() {
/// println!("'{}': '{}'", key, value );
/// }
/// ```
pub fn env() -> Vec<(String,String)> {
env_as_bytes().move_iter().map(|(k,v)| {
let k = str::from_utf8_lossy(k.as_slice()).to_string();
Expand Down Expand Up @@ -277,6 +320,16 @@ pub fn env_as_bytes() -> Vec<(Vec<u8>,Vec<u8>)> {
/// # Failure
///
/// Fails if `n` has any interior NULs.
///
/// # Example
///
/// ```rust
/// let key = "HOME";
/// match std::os::getenv(key) {
/// Some(val) => println!("{}: {}", key, val),
/// None => println!("{} is not defined in the environnement.", key)
/// }
/// ```
pub fn getenv(n: &str) -> Option<String> {
getenv_as_bytes(n).map(|v| str::from_utf8_lossy(v.as_slice()).to_string())
}
Expand Down