Skip to content

libstd: replace all try! with ? in documentation examples #38648

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/libstd/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ pub fn home_dir() -> Option<PathBuf> {
/// let mut dir = env::temp_dir();
/// dir.push("foo.txt");
///
/// let f = try!(File::create(dir));
/// let f = File::create(dir)?;
/// # Ok(())
/// # }
/// ```
Expand Down
110 changes: 55 additions & 55 deletions src/libstd/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ use time::SystemTime;
/// use std::io::prelude::*;
///
/// # fn foo() -> std::io::Result<()> {
/// let mut file = try!(File::create("foo.txt"));
/// try!(file.write_all(b"Hello, world!"));
/// let mut file = File::create("foo.txt")?;
/// file.write_all(b"Hello, world!")?;
/// # Ok(())
/// # }
/// ```
Expand All @@ -55,9 +55,9 @@ use time::SystemTime;
/// use std::io::prelude::*;
///
/// # fn foo() -> std::io::Result<()> {
/// let mut file = try!(File::open("foo.txt"));
/// let mut file = File::open("foo.txt")?;
/// let mut contents = String::new();
/// try!(file.read_to_string(&mut contents));
/// file.read_to_string(&mut contents)?;
/// assert_eq!(contents, "Hello, world!");
/// # Ok(())
/// # }
Expand All @@ -72,10 +72,10 @@ use time::SystemTime;
/// use std::io::prelude::*;
///
/// # fn foo() -> std::io::Result<()> {
/// let file = try!(File::open("foo.txt"));
/// let file = File::open("foo.txt")?;
/// let mut buf_reader = BufReader::new(file);
/// let mut contents = String::new();
/// try!(buf_reader.read_to_string(&mut contents));
/// buf_reader.read_to_string(&mut contents)?;
/// assert_eq!(contents, "Hello, world!");
/// # Ok(())
/// # }
Expand Down Expand Up @@ -227,7 +227,7 @@ impl File {
/// use std::fs::File;
///
/// # fn foo() -> std::io::Result<()> {
/// let mut f = try!(File::open("foo.txt"));
/// let mut f = File::open("foo.txt")?;
/// # Ok(())
/// # }
/// ```
Expand All @@ -251,7 +251,7 @@ impl File {
/// use std::fs::File;
///
/// # fn foo() -> std::io::Result<()> {
/// let mut f = try!(File::create("foo.txt"));
/// let mut f = File::create("foo.txt")?;
/// # Ok(())
/// # }
/// ```
Expand All @@ -272,10 +272,10 @@ impl File {
/// use std::io::prelude::*;
///
/// # fn foo() -> std::io::Result<()> {
/// let mut f = try!(File::create("foo.txt"));
/// try!(f.write_all(b"Hello, world!"));
/// let mut f = File::create("foo.txt")?;
/// f.write_all(b"Hello, world!")?;
///
/// try!(f.sync_all());
/// f.sync_all()?;
/// # Ok(())
/// # }
/// ```
Expand Down Expand Up @@ -303,10 +303,10 @@ impl File {
/// use std::io::prelude::*;
///
/// # fn foo() -> std::io::Result<()> {
/// let mut f = try!(File::create("foo.txt"));
/// try!(f.write_all(b"Hello, world!"));
/// let mut f = File::create("foo.txt")?;
/// f.write_all(b"Hello, world!")?;
///
/// try!(f.sync_data());
/// f.sync_data()?;
/// # Ok(())
/// # }
/// ```
Expand All @@ -333,8 +333,8 @@ impl File {
/// use std::fs::File;
///
/// # fn foo() -> std::io::Result<()> {
/// let mut f = try!(File::create("foo.txt"));
/// try!(f.set_len(10));
/// let mut f = File::create("foo.txt")?;
/// f.set_len(10)?;
/// # Ok(())
/// # }
/// ```
Expand All @@ -351,8 +351,8 @@ impl File {
/// use std::fs::File;
///
/// # fn foo() -> std::io::Result<()> {
/// let mut f = try!(File::open("foo.txt"));
/// let metadata = try!(f.metadata());
/// let mut f = File::open("foo.txt")?;
/// let metadata = f.metadata()?;
/// # Ok(())
/// # }
/// ```
Expand All @@ -373,8 +373,8 @@ impl File {
/// use std::fs::File;
///
/// # fn foo() -> std::io::Result<()> {
/// let mut f = try!(File::open("foo.txt"));
/// let file_copy = try!(f.try_clone());
/// let mut f = File::open("foo.txt")?;
/// let file_copy = f.try_clone()?;
/// # Ok(())
/// # }
/// ```
Expand Down Expand Up @@ -691,7 +691,7 @@ impl Metadata {
/// # fn foo() -> std::io::Result<()> {
/// use std::fs;
///
/// let metadata = try!(fs::metadata("foo.txt"));
/// let metadata = fs::metadata("foo.txt")?;
///
/// println!("{:?}", metadata.file_type());
/// # Ok(())
Expand All @@ -710,7 +710,7 @@ impl Metadata {
/// # fn foo() -> std::io::Result<()> {
/// use std::fs;
///
/// let metadata = try!(fs::metadata("foo.txt"));
/// let metadata = fs::metadata("foo.txt")?;
///
/// assert!(!metadata.is_dir());
/// # Ok(())
Expand All @@ -727,7 +727,7 @@ impl Metadata {
/// # fn foo() -> std::io::Result<()> {
/// use std::fs;
///
/// let metadata = try!(fs::metadata("foo.txt"));
/// let metadata = fs::metadata("foo.txt")?;
///
/// assert!(metadata.is_file());
/// # Ok(())
Expand All @@ -744,7 +744,7 @@ impl Metadata {
/// # fn foo() -> std::io::Result<()> {
/// use std::fs;
///
/// let metadata = try!(fs::metadata("foo.txt"));
/// let metadata = fs::metadata("foo.txt")?;
///
/// assert_eq!(0, metadata.len());
/// # Ok(())
Expand All @@ -761,7 +761,7 @@ impl Metadata {
/// # fn foo() -> std::io::Result<()> {
/// use std::fs;
///
/// let metadata = try!(fs::metadata("foo.txt"));
/// let metadata = fs::metadata("foo.txt")?;
///
/// assert!(!metadata.permissions().readonly());
/// # Ok(())
Expand All @@ -788,7 +788,7 @@ impl Metadata {
/// # fn foo() -> std::io::Result<()> {
/// use std::fs;
///
/// let metadata = try!(fs::metadata("foo.txt"));
/// let metadata = fs::metadata("foo.txt")?;
///
/// if let Ok(time) = metadata.modified() {
/// println!("{:?}", time);
Expand Down Expand Up @@ -823,7 +823,7 @@ impl Metadata {
/// # fn foo() -> std::io::Result<()> {
/// use std::fs;
///
/// let metadata = try!(fs::metadata("foo.txt"));
/// let metadata = fs::metadata("foo.txt")?;
///
/// if let Ok(time) = metadata.accessed() {
/// println!("{:?}", time);
Expand Down Expand Up @@ -854,7 +854,7 @@ impl Metadata {
/// # fn foo() -> std::io::Result<()> {
/// use std::fs;
///
/// let metadata = try!(fs::metadata("foo.txt"));
/// let metadata = fs::metadata("foo.txt")?;
///
/// if let Ok(time) = metadata.created() {
/// println!("{:?}", time);
Expand Down Expand Up @@ -898,8 +898,8 @@ impl Permissions {
/// use std::fs::File;
///
/// # fn foo() -> std::io::Result<()> {
/// let mut f = try!(File::create("foo.txt"));
/// let metadata = try!(f.metadata());
/// let mut f = File::create("foo.txt")?;
/// let metadata = f.metadata()?;
///
/// assert_eq!(false, metadata.permissions().readonly());
/// # Ok(())
Expand All @@ -919,8 +919,8 @@ impl Permissions {
/// use std::fs::File;
///
/// # fn foo() -> std::io::Result<()> {
/// let f = try!(File::create("foo.txt"));
/// let metadata = try!(f.metadata());
/// let f = File::create("foo.txt")?;
/// let metadata = f.metadata()?;
/// let mut permissions = metadata.permissions();
///
/// permissions.set_readonly(true);
Expand Down Expand Up @@ -948,7 +948,7 @@ impl FileType {
/// # fn foo() -> std::io::Result<()> {
/// use std::fs;
///
/// let metadata = try!(fs::metadata("foo.txt"));
/// let metadata = fs::metadata("foo.txt")?;
/// let file_type = metadata.file_type();
///
/// assert_eq!(file_type.is_dir(), false);
Expand All @@ -966,7 +966,7 @@ impl FileType {
/// # fn foo() -> std::io::Result<()> {
/// use std::fs;
///
/// let metadata = try!(fs::metadata("foo.txt"));
/// let metadata = fs::metadata("foo.txt")?;
/// let file_type = metadata.file_type();
///
/// assert_eq!(file_type.is_file(), true);
Expand Down Expand Up @@ -995,7 +995,7 @@ impl FileType {
/// # fn foo() -> std::io::Result<()> {
/// use std::fs;
///
/// let metadata = try!(fs::symlink_metadata("foo.txt"));
/// let metadata = fs::symlink_metadata("foo.txt")?;
/// let file_type = metadata.file_type();
///
/// assert_eq!(file_type.is_symlink(), false);
Expand Down Expand Up @@ -1040,8 +1040,8 @@ impl DirEntry {
/// ```
/// use std::fs;
/// # fn foo() -> std::io::Result<()> {
/// for entry in try!(fs::read_dir(".")) {
/// let dir = try!(entry);
/// for entry in fs::read_dir(".")? {
/// let dir = entry?;
/// println!("{:?}", dir.path());
/// }
/// # Ok(())
Expand Down Expand Up @@ -1193,7 +1193,7 @@ impl AsInner<fs_imp::DirEntry> for DirEntry {
/// use std::fs;
///
/// # fn foo() -> std::io::Result<()> {
/// try!(fs::remove_file("a.txt"));
/// fs::remove_file("a.txt")?;
/// # Ok(())
/// # }
/// ```
Expand Down Expand Up @@ -1229,7 +1229,7 @@ pub fn remove_file<P: AsRef<Path>>(path: P) -> io::Result<()> {
/// # fn foo() -> std::io::Result<()> {
/// use std::fs;
///
/// let attr = try!(fs::metadata("/some/file/path.txt"));
/// let attr = fs::metadata("/some/file/path.txt")?;
/// // inspect attr ...
/// # Ok(())
/// # }
Expand Down Expand Up @@ -1262,7 +1262,7 @@ pub fn metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
/// # fn foo() -> std::io::Result<()> {
/// use std::fs;
///
/// let attr = try!(fs::symlink_metadata("/some/file/path.txt"));
/// let attr = fs::symlink_metadata("/some/file/path.txt")?;
/// // inspect attr ...
/// # Ok(())
/// # }
Expand Down Expand Up @@ -1305,7 +1305,7 @@ pub fn symlink_metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
/// use std::fs;
///
/// # fn foo() -> std::io::Result<()> {
/// try!(fs::rename("a.txt", "b.txt")); // Rename a.txt to b.txt
/// fs::rename("a.txt", "b.txt")?; // Rename a.txt to b.txt
/// # Ok(())
/// # }
/// ```
Expand Down Expand Up @@ -1349,7 +1349,7 @@ pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<()>
/// use std::fs;
///
/// # fn foo() -> std::io::Result<()> {
/// try!(fs::copy("foo.txt", "bar.txt")); // Copy foo.txt to bar.txt
/// fs::copy("foo.txt", "bar.txt")?; // Copy foo.txt to bar.txt
/// # Ok(()) }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -1382,7 +1382,7 @@ pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<u64> {
/// use std::fs;
///
/// # fn foo() -> std::io::Result<()> {
/// try!(fs::hard_link("a.txt", "b.txt")); // Hard link a.txt to b.txt
/// fs::hard_link("a.txt", "b.txt")?; // Hard link a.txt to b.txt
/// # Ok(())
/// # }
/// ```
Expand All @@ -1405,7 +1405,7 @@ pub fn hard_link<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<(
/// use std::fs;
///
/// # fn foo() -> std::io::Result<()> {
/// try!(fs::soft_link("a.txt", "b.txt"));
/// fs::soft_link("a.txt", "b.txt")?;
/// # Ok(())
/// # }
/// ```
Expand Down Expand Up @@ -1441,7 +1441,7 @@ pub fn soft_link<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<(
/// use std::fs;
///
/// # fn foo() -> std::io::Result<()> {
/// let path = try!(fs::read_link("a.txt"));
/// let path = fs::read_link("a.txt")?;
/// # Ok(())
/// # }
/// ```
Expand Down Expand Up @@ -1474,7 +1474,7 @@ pub fn read_link<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
/// use std::fs;
///
/// # fn foo() -> std::io::Result<()> {
/// let path = try!(fs::canonicalize("../a/../foo.txt"));
/// let path = fs::canonicalize("../a/../foo.txt")?;
/// # Ok(())
/// # }
/// ```
Expand Down Expand Up @@ -1506,7 +1506,7 @@ pub fn canonicalize<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
/// use std::fs;
///
/// # fn foo() -> std::io::Result<()> {
/// try!(fs::create_dir("/some/dir"));
/// fs::create_dir("/some/dir")?;
/// # Ok(())
/// # }
/// ```
Expand Down Expand Up @@ -1541,7 +1541,7 @@ pub fn create_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
/// use std::fs;
///
/// # fn foo() -> std::io::Result<()> {
/// try!(fs::create_dir_all("/some/dir"));
/// fs::create_dir_all("/some/dir")?;
/// # Ok(())
/// # }
/// ```
Expand Down Expand Up @@ -1573,7 +1573,7 @@ pub fn create_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
/// use std::fs;
///
/// # fn foo() -> std::io::Result<()> {
/// try!(fs::remove_dir("/some/dir"));
/// fs::remove_dir("/some/dir")?;
/// # Ok(())
/// # }
/// ```
Expand Down Expand Up @@ -1606,7 +1606,7 @@ pub fn remove_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
/// use std::fs;
///
/// # fn foo() -> std::io::Result<()> {
/// try!(fs::remove_dir_all("/some/dir"));
/// fs::remove_dir_all("/some/dir")?;
/// # Ok(())
/// # }
/// ```
Expand Down Expand Up @@ -1649,11 +1649,11 @@ pub fn remove_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
/// // one possible implementation of walking a directory only visiting files
/// fn visit_dirs(dir: &Path, cb: &Fn(&DirEntry)) -> io::Result<()> {
/// if dir.is_dir() {
/// for entry in try!(fs::read_dir(dir)) {
/// let entry = try!(entry);
/// for entry in fs::read_dir(dir)? {
/// let entry = entry?;
/// let path = entry.path();
/// if path.is_dir() {
/// try!(visit_dirs(&path, cb));
/// visit_dirs(&path, cb)?;
/// } else {
/// cb(&entry);
/// }
Expand Down Expand Up @@ -1690,9 +1690,9 @@ pub fn read_dir<P: AsRef<Path>>(path: P) -> io::Result<ReadDir> {
/// # fn foo() -> std::io::Result<()> {
/// use std::fs;
///
/// let mut perms = try!(fs::metadata("foo.txt")).permissions();
/// let mut perms = fs::metadata("foo.txt")?.permissions();
/// perms.set_readonly(true);
/// try!(fs::set_permissions("foo.txt", perms));
/// fs::set_permissions("foo.txt", perms)?;
/// # Ok(())
/// # }
/// ```
Expand Down
Loading