Skip to content

Commit 0c2642a

Browse files
committed
Auto merge of #25666 - tshepang:better-path-docs, r=steveklabnik
2 parents a33b808 + 462829c commit 0c2642a

File tree

1 file changed

+42
-40
lines changed

1 file changed

+42
-40
lines changed

src/libstd/path.rs

+42-40
Original file line numberDiff line numberDiff line change
@@ -1247,9 +1247,10 @@ impl Path {
12471247
/// ```
12481248
/// use std::path::Path;
12491249
///
1250-
/// let s = String::from("bar.txt");
1251-
/// let p = Path::new(&s);
1252-
/// Path::new(&p);
1250+
/// let string = String::from("foo.txt");
1251+
/// let from_string = Path::new(&string);
1252+
/// let from_path = Path::new(&from_string);
1253+
/// assert_eq!(from_string, from_path);
12531254
/// ```
12541255
#[stable(feature = "rust1", since = "1.0.0")]
12551256
pub fn new<S: AsRef<OsStr> + ?Sized>(s: &S) -> &Path {
@@ -1264,6 +1265,7 @@ impl Path {
12641265
/// use std::path::Path;
12651266
///
12661267
/// let os_str = Path::new("foo.txt").as_os_str();
1268+
/// assert_eq!(os_str, std::ffi::OsStr::new("foo.txt"));
12671269
/// ```
12681270
#[stable(feature = "rust1", since = "1.0.0")]
12691271
pub fn as_os_str(&self) -> &OsStr {
@@ -1280,6 +1282,7 @@ impl Path {
12801282
/// use std::path::Path;
12811283
///
12821284
/// let path_str = Path::new("foo.txt").to_str();
1285+
//// assert_eq!(path_str, Some("foo.txt"));
12831286
/// ```
12841287
#[stable(feature = "rust1", since = "1.0.0")]
12851288
pub fn to_str(&self) -> Option<&str> {
@@ -1296,6 +1299,7 @@ impl Path {
12961299
/// use std::path::Path;
12971300
///
12981301
/// let path_str = Path::new("foo.txt").to_string_lossy();
1302+
/// assert_eq!(path_str, "foo.txt");
12991303
/// ```
13001304
#[stable(feature = "rust1", since = "1.0.0")]
13011305
pub fn to_string_lossy(&self) -> Cow<str> {
@@ -1309,7 +1313,8 @@ impl Path {
13091313
/// ```
13101314
/// use std::path::Path;
13111315
///
1312-
/// let path_str = Path::new("foo.txt").to_path_buf();
1316+
/// let path_buf = Path::new("foo.txt").to_path_buf();
1317+
/// assert_eq!(path_buf, std::path::PathBuf::from("foo.txt"));
13131318
/// ```
13141319
#[stable(feature = "rust1", since = "1.0.0")]
13151320
pub fn to_path_buf(&self) -> PathBuf {
@@ -1330,7 +1335,7 @@ impl Path {
13301335
/// ```
13311336
/// use std::path::Path;
13321337
///
1333-
/// assert_eq!(false, Path::new("foo.txt").is_absolute());
1338+
/// assert!(!Path::new("foo.txt").is_absolute());
13341339
/// ```
13351340
#[stable(feature = "rust1", since = "1.0.0")]
13361341
pub fn is_absolute(&self) -> bool {
@@ -1393,14 +1398,12 @@ impl Path {
13931398
/// use std::path::Path;
13941399
///
13951400
/// let path = Path::new("/foo/bar");
1396-
/// let foo = path.parent().unwrap();
1401+
/// let parent = path.parent().unwrap();
1402+
/// assert_eq!(parent, Path::new("/foo"));
13971403
///
1398-
/// assert!(foo == Path::new("/foo"));
1399-
///
1400-
/// let root = foo.parent().unwrap();
1401-
///
1402-
/// assert!(root == Path::new("/"));
1403-
/// assert!(root.parent() == None);
1404+
/// let grand_parent = parent.parent().unwrap();
1405+
/// assert_eq!(grand_parent, Path::new("/"));
1406+
/// assert_eq!(grand_parent.parent(), None);
14041407
/// ```
14051408
#[stable(feature = "rust1", since = "1.0.0")]
14061409
pub fn parent(&self) -> Option<&Path> {
@@ -1416,18 +1419,19 @@ impl Path {
14161419

14171420
/// The final component of the path, if it is a normal file.
14181421
///
1419-
/// If the path terminates in `.`, `..`, or consists solely or a root of
1422+
/// If the path terminates in `.`, `..`, or consists solely of a root of
14201423
/// prefix, `file_name` will return `None`.
14211424
///
14221425
/// # Examples
14231426
///
14241427
/// ```
14251428
/// use std::path::Path;
1429+
/// use std::ffi::OsStr;
14261430
///
1427-
/// let path = Path::new("hello_world.rs");
1428-
/// let filename = "hello_world.rs";
1431+
/// let path = Path::new("foo.txt");
1432+
/// let os_str = OsStr::new("foo.txt");
14291433
///
1430-
/// assert_eq!(filename, path.file_name().unwrap());
1434+
/// assert_eq!(Some(os_str), path.file_name());
14311435
/// ```
14321436
#[stable(feature = "rust1", since = "1.0.0")]
14331437
pub fn file_name(&self) -> Option<&OsStr> {
@@ -1538,11 +1542,9 @@ impl Path {
15381542
/// # Examples
15391543
///
15401544
/// ```
1541-
/// use std::path::Path;
1542-
///
1543-
/// let path = Path::new("/tmp");
1545+
/// use std::path::{Path, PathBuf};
15441546
///
1545-
/// let new_path = path.join("foo");
1547+
/// assert_eq!(Path::new("/etc").join("passwd"), PathBuf::from("/etc/passwd"));
15461548
/// ```
15471549
#[stable(feature = "rust1", since = "1.0.0")]
15481550
pub fn join<P: AsRef<Path>>(&self, path: P) -> PathBuf {
@@ -1558,11 +1560,10 @@ impl Path {
15581560
/// # Examples
15591561
///
15601562
/// ```
1561-
/// use std::path::Path;
1562-
///
1563-
/// let path = Path::new("/tmp/foo.rs");
1563+
/// use std::path::{Path, PathBuf};
15641564
///
1565-
/// let new_path = path.with_file_name("bar.rs");
1565+
/// let path = Path::new("/tmp/foo.txt");
1566+
/// assert_eq!(path.with_file_name("bar.txt"), PathBuf::from("/tmp/bar.txt"));
15661567
/// ```
15671568
#[stable(feature = "rust1", since = "1.0.0")]
15681569
pub fn with_file_name<S: AsRef<OsStr>>(&self, file_name: S) -> PathBuf {
@@ -1580,10 +1581,8 @@ impl Path {
15801581
/// ```
15811582
/// use std::path::{Path, PathBuf};
15821583
///
1583-
/// let path = Path::new("/tmp/foo.rs");
1584-
///
1585-
/// let new_path = path.with_extension("txt");
1586-
/// assert_eq!(new_path, PathBuf::from("/tmp/foo.txt"));
1584+
/// let path = Path::new("foo.rs");
1585+
/// assert_eq!(path.with_extension("txt"), PathBuf::from("foo.txt"));
15871586
/// ```
15881587
#[stable(feature = "rust1", since = "1.0.0")]
15891588
pub fn with_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf {
@@ -1597,13 +1596,15 @@ impl Path {
15971596
/// # Examples
15981597
///
15991598
/// ```
1600-
/// use std::path::Path;
1599+
/// use std::path::{Path, Component};
1600+
/// use std::ffi::OsStr;
16011601
///
1602-
/// let path = Path::new("/tmp/foo.rs");
1602+
/// let mut components = Path::new("/tmp/foo.txt").components();
16031603
///
1604-
/// for component in path.components() {
1605-
/// println!("{:?}", component);
1606-
/// }
1604+
/// assert_eq!(components.next(), Some(Component::RootDir));
1605+
/// assert_eq!(components.next(), Some(Component::Normal(OsStr::new("tmp"))));
1606+
/// assert_eq!(components.next(), Some(Component::Normal(OsStr::new("foo.txt"))));
1607+
/// assert_eq!(components.next(), None)
16071608
/// ```
16081609
#[stable(feature = "rust1", since = "1.0.0")]
16091610
pub fn components(&self) -> Components {
@@ -1622,13 +1623,14 @@ impl Path {
16221623
/// # Examples
16231624
///
16241625
/// ```
1625-
/// use std::path::Path;
1626-
///
1627-
/// let path = Path::new("/tmp/foo.rs");
1628-
///
1629-
/// for component in path.iter() {
1630-
/// println!("{:?}", component);
1631-
/// }
1626+
/// use std::path::{self, Path};
1627+
/// use std::ffi::OsStr;
1628+
///
1629+
/// let mut it = Path::new("/tmp/foo.txt").iter();
1630+
/// assert_eq!(it.next(), Some(OsStr::new(&path::MAIN_SEPARATOR.to_string())));
1631+
/// assert_eq!(it.next(), Some(OsStr::new("tmp")));
1632+
/// assert_eq!(it.next(), Some(OsStr::new("foo.txt")));
1633+
/// assert_eq!(it.next(), None)
16321634
/// ```
16331635
#[stable(feature = "rust1", since = "1.0.0")]
16341636
pub fn iter(&self) -> Iter {

0 commit comments

Comments
 (0)