You've already forked comprehensive-rust
mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-07-05 14:10:29 +02:00
Simplify imports by importing fewer items directly (#2404)
When reading the code without an IDE, I find it useful to use explicit module names, especially when items come from the standard library. So `io::Error` instead of just `Error`, especially when people have just been told about `std::error::Error` as well. I also omitted most single-use items: I find it has less cognitive overhead to say “we import `fmt`” and then later use `fmt::Display` and `fmt::Formatter` in the code. It’s clear from the name that these two things have something to do with formatting. Finally, I made a few usages more consistent so that we refer to each item in the same way within a single codeblock.
This commit is contained in:
@ -9,8 +9,8 @@ avoid boilerplate when defining error types. It provides derive macros that
|
||||
assist in implementing `From<T>`, `Display`, and the `Error` trait.
|
||||
|
||||
```rust,editable,compile_fail
|
||||
use std::fs;
|
||||
use std::io::{self, Read};
|
||||
use std::io::Read;
|
||||
use std::{fs, io};
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
|
@ -28,9 +28,8 @@ higher-level errors.
|
||||
|
||||
```rust,editable
|
||||
use std::error::Error;
|
||||
use std::fmt::{self, Display, Formatter};
|
||||
use std::fs::File;
|
||||
use std::io::{self, Read};
|
||||
use std::io::Read;
|
||||
use std::{fmt, fs, io};
|
||||
|
||||
#[derive(Debug)]
|
||||
enum ReadUsernameError {
|
||||
@ -40,8 +39,8 @@ enum ReadUsernameError {
|
||||
|
||||
impl Error for ReadUsernameError {}
|
||||
|
||||
impl Display for ReadUsernameError {
|
||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||
impl fmt::Display for ReadUsernameError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Self::IoError(e) => write!(f, "I/O error: {e}"),
|
||||
Self::EmptyUsername(path) => write!(f, "Found no username in {path}"),
|
||||
@ -57,7 +56,7 @@ impl From<io::Error> for ReadUsernameError {
|
||||
|
||||
fn read_username(path: &str) -> Result<String, ReadUsernameError> {
|
||||
let mut username = String::with_capacity(100);
|
||||
File::open(path)?.read_to_string(&mut username)?;
|
||||
fs::File::open(path)?.read_to_string(&mut username)?;
|
||||
if username.is_empty() {
|
||||
return Err(ReadUsernameError::EmptyUsername(String::from(path)));
|
||||
}
|
||||
|
Reference in New Issue
Block a user