Skip to content

Commit d3e116a

Browse files
authored
Rollup merge of #42749 - frewsxcv:frewsxcxv/doc-examples, r=QuietMisdreavus
Additions/improvements for doc examples. None
2 parents d92edac + 58bbe1d commit d3e116a

File tree

2 files changed

+172
-0
lines changed

2 files changed

+172
-0
lines changed

src/libstd/env.rs

+29
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,35 @@ pub struct JoinPathsError {
438438
///
439439
/// # Examples
440440
///
441+
/// Joining paths on a Unix-like platform:
442+
///
443+
/// ```
444+
/// # if cfg!(unix) {
445+
/// use std::env;
446+
/// use std::ffi::OsString;
447+
/// use std::path::Path;
448+
///
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"));
452+
/// # }
453+
/// ```
454+
///
455+
/// Joining a path containing a colon on a Unix-like platform results in an error:
456+
///
457+
/// ```
458+
/// # if cfg!(unix) {
459+
/// use std::env;
460+
/// use std::path::Path;
461+
///
462+
/// let paths = [Path::new("/bin"), Path::new("/usr/bi:n")];
463+
/// assert!(env::join_paths(paths.iter()).is_err());
464+
/// # }
465+
/// ```
466+
///
467+
/// Using `env::join_paths` with `env::spit_paths` to append an item to the `PATH` environment
468+
/// variable:
469+
///
441470
/// ```
442471
/// use std::env;
443472
/// use std::path::PathBuf;

src/libstd/ffi/c_str.rs

+143
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ pub struct CStr {
152152
/// in the vector provided.
153153
///
154154
/// [`CString::new`]: struct.CString.html#method.new
155+
///
156+
/// # Examples
157+
///
158+
/// ```
159+
/// use std::ffi::{CString, NulError};
160+
///
161+
/// let _: NulError = CString::new(b"f\0oo".to_vec()).unwrap_err();
162+
/// ```
155163
#[derive(Clone, PartialEq, Eq, Debug)]
156164
#[stable(feature = "rust1", since = "1.0.0")]
157165
pub struct NulError(usize, Vec<u8>);
@@ -160,6 +168,14 @@ pub struct NulError(usize, Vec<u8>);
160168
/// byte was found too early in the slice provided or one wasn't found at all.
161169
///
162170
/// [`CStr::from_bytes_with_nul`]: struct.CStr.html#method.from_bytes_with_nul
171+
///
172+
/// # Examples
173+
///
174+
/// ```
175+
/// use std::ffi::{CStr, FromBytesWithNulError};
176+
///
177+
/// let _: FromBytesWithNulError = CStr::from_bytes_with_nul(b"f\0oo").unwrap_err();
178+
/// ```
163179
#[derive(Clone, PartialEq, Eq, Debug)]
164180
#[stable(feature = "cstr_from_bytes", since = "1.10.0")]
165181
pub struct FromBytesWithNulError {
@@ -271,6 +287,27 @@ impl CString {
271287
/// to undefined behavior or allocator corruption.
272288
///
273289
/// [`into_raw`]: #method.into_raw
290+
///
291+
/// # Examples
292+
///
293+
/// Create a `CString`, pass ownership to an `extern` function (via raw pointer), then retake
294+
/// ownership with `from_raw`:
295+
///
296+
/// ```no_run
297+
/// use std::ffi::CString;
298+
/// use std::os::raw::c_char;
299+
///
300+
/// extern {
301+
/// fn some_extern_function(s: *mut c_char);
302+
/// }
303+
///
304+
/// let c_string = CString::new("Hello!").unwrap();
305+
/// let raw = c_string.into_raw();
306+
/// unsafe {
307+
/// some_extern_function(raw);
308+
/// let c_string = CString::from_raw(raw);
309+
/// }
310+
/// ```
274311
#[stable(feature = "cstr_memory", since = "1.4.0")]
275312
pub unsafe fn from_raw(ptr: *mut c_char) -> CString {
276313
let len = libc::strlen(ptr) + 1; // Including the NUL byte
@@ -412,6 +449,18 @@ impl CString {
412449
/// Extracts a [`CStr`] slice containing the entire string.
413450
///
414451
/// [`CStr`]: struct.CStr.html
452+
///
453+
/// # Examples
454+
///
455+
/// ```
456+
/// #![feature(as_c_str)]
457+
///
458+
/// use std::ffi::{CString, CStr};
459+
///
460+
/// let c_string = CString::new(b"foo".to_vec()).unwrap();
461+
/// let c_str = c_string.as_c_str();
462+
/// assert_eq!(c_str, CStr::from_bytes_with_nul(b"foo\0").unwrap());
463+
/// ```
415464
#[inline]
416465
#[unstable(feature = "as_c_str", issue = "40380")]
417466
pub fn as_c_str(&self) -> &CStr {
@@ -421,6 +470,18 @@ impl CString {
421470
/// Converts this `CString` into a boxed [`CStr`].
422471
///
423472
/// [`CStr`]: struct.CStr.html
473+
///
474+
/// # Examples
475+
///
476+
/// ```
477+
/// #![feature(into_boxed_c_str)]
478+
///
479+
/// use std::ffi::{CString, CStr};
480+
///
481+
/// let c_string = CString::new(b"foo".to_vec()).unwrap();
482+
/// let boxed = c_string.into_boxed_c_str();
483+
/// assert_eq!(&*boxed, CStr::from_bytes_with_nul(b"foo\0").unwrap());
484+
/// ```
424485
#[unstable(feature = "into_boxed_c_str", issue = "40380")]
425486
pub fn into_boxed_c_str(self) -> Box<CStr> {
426487
unsafe { mem::transmute(self.into_inner()) }
@@ -708,6 +769,24 @@ impl CStr {
708769
/// let cstr = CStr::from_bytes_with_nul(b"hello\0");
709770
/// assert!(cstr.is_ok());
710771
/// ```
772+
///
773+
/// Creating a `CStr` without a trailing nul byte is an error:
774+
///
775+
/// ```
776+
/// use std::ffi::CStr;
777+
///
778+
/// let c_str = CStr::from_bytes_with_nul(b"hello");
779+
/// assert!(c_str.is_err());
780+
/// ```
781+
///
782+
/// Creating a `CStr` with an interior nul byte is an error:
783+
///
784+
/// ```
785+
/// use std::ffi::CStr;
786+
///
787+
/// let c_str = CStr::from_bytes_with_nul(b"he\0llo\0");
788+
/// assert!(c_str.is_err());
789+
/// ```
711790
#[stable(feature = "cstr_from_bytes", since = "1.10.0")]
712791
pub fn from_bytes_with_nul(bytes: &[u8])
713792
-> Result<&CStr, FromBytesWithNulError> {
@@ -800,6 +879,15 @@ impl CStr {
800879
/// > **Note**: This method is currently implemented as a 0-cost cast, but
801880
/// > it is planned to alter its definition in the future to perform the
802881
/// > length calculation whenever this method is called.
882+
///
883+
/// # Examples
884+
///
885+
/// ```
886+
/// use std::ffi::CStr;
887+
///
888+
/// let c_str = CStr::from_bytes_with_nul(b"foo\0").unwrap();
889+
/// assert_eq!(c_str.to_bytes(), b"foo");
890+
/// ```
803891
#[inline]
804892
#[stable(feature = "rust1", since = "1.0.0")]
805893
pub fn to_bytes(&self) -> &[u8] {
@@ -817,6 +905,15 @@ impl CStr {
817905
/// > length calculation whenever this method is called.
818906
///
819907
/// [`to_bytes`]: #method.to_bytes
908+
///
909+
/// # Examples
910+
///
911+
/// ```
912+
/// use std::ffi::CStr;
913+
///
914+
/// let c_str = CStr::from_bytes_with_nul(b"foo\0").unwrap();
915+
/// assert_eq!(c_str.to_bytes_with_nul(), b"foo\0");
916+
/// ```
820917
#[inline]
821918
#[stable(feature = "rust1", since = "1.0.0")]
822919
pub fn to_bytes_with_nul(&self) -> &[u8] {
@@ -834,6 +931,15 @@ impl CStr {
834931
/// > check whenever this method is called.
835932
///
836933
/// [`&str`]: ../primitive.str.html
934+
///
935+
/// # Examples
936+
///
937+
/// ```
938+
/// use std::ffi::CStr;
939+
///
940+
/// let c_str = CStr::from_bytes_with_nul(b"foo\0").unwrap();
941+
/// assert_eq!(c_str.to_str(), Ok("foo"));
942+
/// ```
837943
#[stable(feature = "cstr_to_str", since = "1.4.0")]
838944
pub fn to_str(&self) -> Result<&str, str::Utf8Error> {
839945
// NB: When CStr is changed to perform the length check in .to_bytes()
@@ -857,6 +963,31 @@ impl CStr {
857963
///
858964
/// [`Cow`]: ../borrow/enum.Cow.html
859965
/// [`str`]: ../primitive.str.html
966+
///
967+
/// # Examples
968+
///
969+
/// Calling `to_string_lossy` on a `CStr` containing valid UTF-8:
970+
///
971+
/// ```
972+
/// use std::borrow::Cow;
973+
/// use std::ffi::CStr;
974+
///
975+
/// let c_str = CStr::from_bytes_with_nul(b"Hello World\0").unwrap();
976+
/// assert_eq!(c_str.to_string_lossy(), Cow::Borrowed("Hello World"));
977+
/// ```
978+
///
979+
/// Calling `to_string_lossy` on a `CStr` containing invalid UTF-8:
980+
///
981+
/// ```
982+
/// use std::borrow::Cow;
983+
/// use std::ffi::CStr;
984+
///
985+
/// let c_str = CStr::from_bytes_with_nul(b"Hello \xF0\x90\x80World\0").unwrap();
986+
/// assert_eq!(
987+
/// c_str.to_string_lossy(),
988+
/// Cow::Owned(String::from("Hello �World")) as Cow<str>
989+
/// );
990+
/// ```
860991
#[stable(feature = "cstr_to_str", since = "1.4.0")]
861992
pub fn to_string_lossy(&self) -> Cow<str> {
862993
String::from_utf8_lossy(self.to_bytes())
@@ -866,6 +997,18 @@ impl CStr {
866997
///
867998
/// [`Box`]: ../boxed/struct.Box.html
868999
/// [`CString`]: struct.CString.html
1000+
///
1001+
/// # Examples
1002+
///
1003+
/// ```
1004+
/// #![feature(into_boxed_c_str)]
1005+
///
1006+
/// use std::ffi::CString;
1007+
///
1008+
/// let c_string = CString::new(b"foo".to_vec()).unwrap();
1009+
/// let boxed = c_string.into_boxed_c_str();
1010+
/// assert_eq!(boxed.into_c_string(), CString::new("foo").unwrap());
1011+
/// ```
8691012
#[unstable(feature = "into_boxed_c_str", issue = "40380")]
8701013
pub fn into_c_string(self: Box<CStr>) -> CString {
8711014
unsafe { mem::transmute(self) }

0 commit comments

Comments
 (0)