Skip to content

Commit 4794f95

Browse files
committed
Expand {Path,OsStr}::{to_str,to_string_lossy} doc examples.
1 parent 7b659cf commit 4794f95

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

src/libstd/ffi/os_str.rs

+23
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,15 @@ impl OsStr {
259259
/// Yields a `&str` slice if the `OsStr` is valid Unicode.
260260
///
261261
/// This conversion may entail doing a check for UTF-8 validity.
262+
///
263+
/// # Examples
264+
///
265+
/// ```
266+
/// use std::ffi::OsStr;
267+
///
268+
/// let os_str = OsStr::new("foo");
269+
/// assert_eq!(os_str.to_str(), Some("foo"));
270+
/// ```
262271
#[stable(feature = "rust1", since = "1.0.0")]
263272
pub fn to_str(&self) -> Option<&str> {
264273
self.inner.to_str()
@@ -267,6 +276,20 @@ impl OsStr {
267276
/// Converts an `OsStr` to a `Cow<str>`.
268277
///
269278
/// Any non-Unicode sequences are replaced with U+FFFD REPLACEMENT CHARACTER.
279+
///
280+
/// # Examples
281+
///
282+
/// Calling `to_string_lossy` on an `OsStr` with valid unicode:
283+
///
284+
/// ```
285+
/// use std::ffi::OsStr;
286+
///
287+
/// let os_str = OsStr::new("foo");
288+
/// assert_eq!(os_str.to_string_lossy(), "foo");
289+
/// ```
290+
///
291+
/// Had `os_str` contained invalid unicode, the `to_string_lossy` call might
292+
/// have returned `"fo�"`.
270293
#[stable(feature = "rust1", since = "1.0.0")]
271294
pub fn to_string_lossy(&self) -> Cow<str> {
272295
self.inner.to_string_lossy()

src/libstd/path.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -1428,8 +1428,8 @@ impl Path {
14281428
/// ```
14291429
/// use std::path::Path;
14301430
///
1431-
/// let path_str = Path::new("foo.txt").to_str();
1432-
/// assert_eq!(path_str, Some("foo.txt"));
1431+
/// let path = Path::new("foo.txt");
1432+
/// assert_eq!(path.to_str(), Some("foo.txt"));
14331433
/// ```
14341434
#[stable(feature = "rust1", since = "1.0.0")]
14351435
pub fn to_str(&self) -> Option<&str> {
@@ -1444,12 +1444,17 @@ impl Path {
14441444
///
14451445
/// # Examples
14461446
///
1447+
/// Calling `to_string_lossy` on a `Path` with valid unicode:
1448+
///
14471449
/// ```
14481450
/// use std::path::Path;
14491451
///
1450-
/// let path_str = Path::new("foo.txt").to_string_lossy();
1451-
/// assert_eq!(path_str, "foo.txt");
1452+
/// let path = Path::new("foo.txt");
1453+
/// assert_eq!(path.to_string_lossy(), "foo.txt");
14521454
/// ```
1455+
///
1456+
/// Had `os_str` contained invalid unicode, the `to_string_lossy` call might
1457+
/// have returned `"fo�.txt"`.
14531458
#[stable(feature = "rust1", since = "1.0.0")]
14541459
pub fn to_string_lossy(&self) -> Cow<str> {
14551460
self.inner.to_string_lossy()

0 commit comments

Comments
 (0)