Skip to content

Commit 06e6173

Browse files
committed
Rollup merge of rust-lang#50736 - udoprog:env-try-op, r=shepmaster
env: remove unwrap in examples in favor of try op
2 parents 539a376 + f73c4a4 commit 06e6173

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

src/libstd/env.rs

+21-12
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,11 @@ use sys::os as os_imp;
4949
/// ```
5050
/// use std::env;
5151
///
52-
/// // We assume that we are in a valid directory.
53-
/// let path = env::current_dir().unwrap();
54-
/// println!("The current directory is {}", path.display());
52+
/// fn main() -> std::io::Result<()> {
53+
/// let path = env::current_dir()?;
54+
/// println!("The current directory is {}", path.display());
55+
/// Ok(())
56+
/// }
5557
/// ```
5658
#[stable(feature = "env", since = "1.0.0")]
5759
pub fn current_dir() -> io::Result<PathBuf> {
@@ -441,15 +443,18 @@ pub struct JoinPathsError {
441443
/// Joining paths on a Unix-like platform:
442444
///
443445
/// ```
444-
/// # if cfg!(unix) {
445446
/// use std::env;
446447
/// use std::ffi::OsString;
447448
/// use std::path::Path;
448449
///
449-
/// let paths = [Path::new("/bin"), Path::new("/usr/bin")];
450-
/// let path_os_string = env::join_paths(paths.iter()).unwrap();
451-
/// assert_eq!(path_os_string, OsString::from("/bin:/usr/bin"));
450+
/// fn main() -> Result<(), env::JoinPathsError> {
451+
/// # if cfg!(unix) {
452+
/// let paths = [Path::new("/bin"), Path::new("/usr/bin")];
453+
/// let path_os_string = env::join_paths(paths.iter())?;
454+
/// assert_eq!(path_os_string, OsString::from("/bin:/usr/bin"));
452455
/// # }
456+
/// Ok(())
457+
/// }
453458
/// ```
454459
///
455460
/// Joining a path containing a colon on a Unix-like platform results in an error:
@@ -471,11 +476,15 @@ pub struct JoinPathsError {
471476
/// use std::env;
472477
/// use std::path::PathBuf;
473478
///
474-
/// if let Some(path) = env::var_os("PATH") {
475-
/// let mut paths = env::split_paths(&path).collect::<Vec<_>>();
476-
/// paths.push(PathBuf::from("/home/xyz/bin"));
477-
/// let new_path = env::join_paths(paths).unwrap();
478-
/// env::set_var("PATH", &new_path);
479+
/// fn main() -> Result<(), env::JoinPathsError> {
480+
/// if let Some(path) = env::var_os("PATH") {
481+
/// let mut paths = env::split_paths(&path).collect::<Vec<_>>();
482+
/// paths.push(PathBuf::from("/home/xyz/bin"));
483+
/// let new_path = env::join_paths(paths)?;
484+
/// env::set_var("PATH", &new_path);
485+
/// }
486+
///
487+
/// Ok(())
479488
/// }
480489
/// ```
481490
#[stable(feature = "env", since = "1.0.0")]

0 commit comments

Comments
 (0)