Skip to content

Commit 1d7c916

Browse files
authored
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.
1 parent 7a462ef commit 1d7c916

File tree

5 files changed

+16
-18
lines changed

5 files changed

+16
-18
lines changed

src/concurrency/async-control-flow/channels.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ minutes: 8
77
Several crates have support for asynchronous channels. For instance `tokio`:
88

99
```rust,editable,compile_fail
10-
use tokio::sync::mpsc::{self, Receiver};
10+
use tokio::sync::mpsc;
1111
12-
async fn ping_handler(mut input: Receiver<()>) {
12+
async fn ping_handler(mut input: mpsc::Receiver<()>) {
1313
let mut count: usize = 0;
1414
1515
while let Some(_) = input.recv().await {

src/concurrency/async-exercises/dining-philosophers.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
// ANCHOR: solution
1616
// ANCHOR: Philosopher
1717
use std::sync::Arc;
18-
use tokio::sync::mpsc::{self, Sender};
19-
use tokio::sync::Mutex;
18+
use tokio::sync::{mpsc, Mutex};
2019
use tokio::time;
2120

2221
struct Fork;
@@ -26,7 +25,7 @@ struct Philosopher {
2625
// ANCHOR_END: Philosopher
2726
left_fork: Arc<Mutex<Fork>>,
2827
right_fork: Arc<Mutex<Fork>>,
29-
thoughts: Sender<String>,
28+
thoughts: mpsc::Sender<String>,
3029
}
3130

3231
// ANCHOR: Philosopher-think

src/concurrency/async-pitfalls/cancellation.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ the system works correctly even when futures are cancelled. For example, it
1010
shouldn't deadlock or lose data.
1111

1212
```rust,editable,compile_fail
13-
use std::io::{self, ErrorKind};
13+
use std::io;
1414
use std::time::Duration;
1515
use tokio::io::{AsyncReadExt, AsyncWriteExt, DuplexStream};
1616
@@ -36,12 +36,12 @@ impl LinesReader {
3636
return Ok(None);
3737
}
3838
let s = String::from_utf8(bytes)
39-
.map_err(|_| io::Error::new(ErrorKind::InvalidData, "not UTF-8"))?;
39+
.map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "not UTF-8"))?;
4040
Ok(Some(s))
4141
}
4242
}
4343
44-
async fn slow_copy(source: String, mut dest: DuplexStream) -> std::io::Result<()> {
44+
async fn slow_copy(source: String, mut dest: DuplexStream) -> io::Result<()> {
4545
for b in source.bytes() {
4646
dest.write_u8(b).await?;
4747
tokio::time::sleep(Duration::from_millis(10)).await
@@ -50,7 +50,7 @@ async fn slow_copy(source: String, mut dest: DuplexStream) -> std::io::Result<()
5050
}
5151
5252
#[tokio::main]
53-
async fn main() -> std::io::Result<()> {
53+
async fn main() -> io::Result<()> {
5454
let (client, server) = tokio::io::duplex(5);
5555
let handle = tokio::spawn(slow_copy("hi\nthere\n".to_owned(), client));
5656
@@ -102,7 +102,7 @@ async fn main() -> std::io::Result<()> {
102102
// ...
103103
let raw = std::mem::take(&mut self.bytes);
104104
let s = String::from_utf8(raw)
105-
.map_err(|_| io::Error::new(ErrorKind::InvalidData, "not UTF-8"))?;
105+
.map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "not UTF-8"))?;
106106
// ...
107107
}
108108
}

src/error-handling/thiserror.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ avoid boilerplate when defining error types. It provides derive macros that
99
assist in implementing `From<T>`, `Display`, and the `Error` trait.
1010

1111
```rust,editable,compile_fail
12-
use std::fs;
13-
use std::io::{self, Read};
12+
use std::io::Read;
13+
use std::{fs, io};
1414
use thiserror::Error;
1515
1616
#[derive(Debug, Error)]

src/error-handling/try-conversions.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ higher-level errors.
2828

2929
```rust,editable
3030
use std::error::Error;
31-
use std::fmt::{self, Display, Formatter};
32-
use std::fs::File;
33-
use std::io::{self, Read};
31+
use std::io::Read;
32+
use std::{fmt, fs, io};
3433
3534
#[derive(Debug)]
3635
enum ReadUsernameError {
@@ -40,8 +39,8 @@ enum ReadUsernameError {
4039
4140
impl Error for ReadUsernameError {}
4241
43-
impl Display for ReadUsernameError {
44-
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
42+
impl fmt::Display for ReadUsernameError {
43+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4544
match self {
4645
Self::IoError(e) => write!(f, "I/O error: {e}"),
4746
Self::EmptyUsername(path) => write!(f, "Found no username in {path}"),
@@ -57,7 +56,7 @@ impl From<io::Error> for ReadUsernameError {
5756
5857
fn read_username(path: &str) -> Result<String, ReadUsernameError> {
5958
let mut username = String::with_capacity(100);
60-
File::open(path)?.read_to_string(&mut username)?;
59+
fs::File::open(path)?.read_to_string(&mut username)?;
6160
if username.is_empty() {
6261
return Err(ReadUsernameError::EmptyUsername(String::from(path)));
6362
}

0 commit comments

Comments
 (0)