Skip to content

Commit 5ec49c7

Browse files
committed
auto merge of #14641 : darnuria/rust/add_documentation_to_std_os, r=alexcrichton
Just opening a pull request for adding code examples and documentation to std::os. More to come soon.
2 parents ba3ba00 + 85adc09 commit 5ec49c7

File tree

1 file changed

+57
-4
lines changed

1 file changed

+57
-4
lines changed

src/libstd/os.rs

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,24 @@ pub fn close(fd: int) -> int {
6666
pub static TMPBUF_SZ : uint = 1000u;
6767
static BUF_BYTES : uint = 2048u;
6868

69-
/// Returns the current working directory.
69+
/// Returns the current working directory as a Path.
70+
///
71+
/// # Failure
72+
///
73+
/// Fails if the current working directory value is invalid:
74+
/// Possibles cases:
75+
///
76+
/// * Current directory does not exist.
77+
/// * There are insufficient permissions to access the current directory.
78+
///
79+
/// # Example
80+
///
81+
/// ```rust
82+
/// // We assume that we are in a valid directory like "/home".
83+
/// let current_working_directory = std::os::getcwd();
84+
/// println!("The current directory is {}", current_working_directory.display());
85+
/// // /home
86+
/// ```
7087
#[cfg(unix)]
7188
pub fn getcwd() -> Path {
7289
use c_str::CString;
@@ -80,7 +97,24 @@ pub fn getcwd() -> Path {
8097
}
8198
}
8299

83-
/// Returns the current working directory.
100+
/// Returns the current working directory as a Path.
101+
///
102+
/// # Failure
103+
///
104+
/// Fails if the current working directory value is invalid.
105+
/// Possibles cases:
106+
///
107+
/// * Current directory does not exist.
108+
/// * There are insufficient permissions to access the current directory.
109+
///
110+
/// # Example
111+
///
112+
/// ```rust
113+
/// // We assume that we are in a valid directory like "C:\\Windows".
114+
/// let current_working_directory = std::os::getcwd();
115+
/// println!("The current directory is {}", current_working_directory.display());
116+
/// // C:\\Windows
117+
/// ```
84118
#[cfg(windows)]
85119
pub fn getcwd() -> Path {
86120
use libc::DWORD;
@@ -171,11 +205,20 @@ fn with_env_lock<T>(f: || -> T) -> T {
171205
}
172206
}
173207

174-
/// Returns a vector of (variable, value) pairs for all the environment
175-
/// variables of the current process.
208+
/// Returns a vector of (variable, value) pairs as a Vec<(String, String)>,
209+
/// for all the environment variables of the current process.
176210
///
177211
/// Invalid UTF-8 bytes are replaced with \uFFFD. See `str::from_utf8_lossy()`
178212
/// for details.
213+
///
214+
/// # Example
215+
///
216+
/// ```rust
217+
/// // We will iterate through the references to the element returned by std::os::env();
218+
/// for &(ref key, ref value) in std::os::env().iter() {
219+
/// println!("'{}': '{}'", key, value );
220+
/// }
221+
/// ```
179222
pub fn env() -> Vec<(String,String)> {
180223
env_as_bytes().move_iter().map(|(k,v)| {
181224
let k = str::from_utf8_lossy(k.as_slice()).to_string();
@@ -276,6 +319,16 @@ pub fn env_as_bytes() -> Vec<(Vec<u8>,Vec<u8>)> {
276319
/// # Failure
277320
///
278321
/// Fails if `n` has any interior NULs.
322+
///
323+
/// # Example
324+
///
325+
/// ```rust
326+
/// let key = "HOME";
327+
/// match std::os::getenv(key) {
328+
/// Some(val) => println!("{}: {}", key, val),
329+
/// None => println!("{} is not defined in the environnement.", key)
330+
/// }
331+
/// ```
279332
pub fn getenv(n: &str) -> Option<String> {
280333
getenv_as_bytes(n).map(|v| str::from_utf8_lossy(v.as_slice()).to_string())
281334
}

0 commit comments

Comments
 (0)