Skip to content

Commit 77c4b3d

Browse files
author
Anton Lofgren
committed
libstd: Add a few doc examples
This patch adds doc examples for the make_absolute, change_dir, errors_string and args functions in the os module.
1 parent 88231a9 commit 77c4b3d

File tree

1 file changed

+45
-7
lines changed

1 file changed

+45
-7
lines changed

src/libstd/os.rs

+45-7
Original file line numberDiff line numberDiff line change
@@ -837,13 +837,24 @@ pub fn tmpdir() -> Path {
837837
}
838838
}
839839

840-
/**
841-
* Convert a relative path to an absolute path
842-
*
843-
* If the given path is relative, return it prepended with the current working
844-
* directory. If the given path is already an absolute path, return it
845-
* as is.
846-
*/
840+
///
841+
/// Convert a relative path to an absolute path
842+
///
843+
/// If the given path is relative, return it prepended with the current working
844+
/// directory. If the given path is already an absolute path, return it
845+
/// as is.
846+
///
847+
/// # Example
848+
/// ```rust
849+
/// use std::os;
850+
/// use std::path::Path;
851+
///
852+
/// // Assume we're in a path like /home/someuser
853+
/// let rel_path = Path::new("..");
854+
/// let abs_path = os::make_absolute(&rel_path);
855+
/// println!("The absolute path is {}", abs_path.display());
856+
/// // Prints "The absolute path is /home"
857+
/// ```
847858
// NB: this is here rather than in path because it is a form of environment
848859
// querying; what it does depends on the process working directory, not just
849860
// the input paths.
@@ -859,6 +870,16 @@ pub fn make_absolute(p: &Path) -> Path {
859870

860871
/// Changes the current working directory to the specified path, returning
861872
/// whether the change was completed successfully or not.
873+
///
874+
/// # Example
875+
/// ```rust
876+
/// use std::os;
877+
/// use std::path::Path;
878+
///
879+
/// let root = Path::new("/");
880+
/// assert!(os::change_dir(&root));
881+
/// println!("Succesfully changed working directory to {}!", root.display());
882+
/// ```
862883
pub fn change_dir(p: &Path) -> bool {
863884
return chdir(p);
864885

@@ -930,6 +951,13 @@ pub fn errno() -> uint {
930951
}
931952

932953
/// Return the string corresponding to an `errno()` value of `errnum`.
954+
/// # Example
955+
/// ```rust
956+
/// use std::os;
957+
///
958+
/// // Same as println!("{}", last_os_error());
959+
/// println!("{}", os::error_string(os::errno() as uint));
960+
/// ```
933961
pub fn error_string(errnum: uint) -> String {
934962
return strerror(errnum);
935963

@@ -1217,6 +1245,16 @@ extern "system" {
12171245
///
12181246
/// The arguments are interpreted as utf-8, with invalid bytes replaced with \uFFFD.
12191247
/// See `str::from_utf8_lossy` for details.
1248+
/// # Example
1249+
///
1250+
/// ```rust
1251+
/// use std::os;
1252+
///
1253+
/// // Prints each argument on a separate line
1254+
/// for argument in os::args().iter() {
1255+
/// println!("{}", argument);
1256+
/// }
1257+
/// ```
12201258
pub fn args() -> Vec<String> {
12211259
real_args()
12221260
}

0 commit comments

Comments
 (0)