Skip to content

Commit a10d243

Browse files
committed
Use byte string literal syntax
The byte string literal syntax `b"whatever"` is more idiomatic than `"whatever".as_bytes()`.
1 parent 55c8bac commit a10d243

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/doc/trpl/traits.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,17 +183,17 @@ won’t have its methods:
183183

184184
```rust,ignore
185185
let mut f = std::fs::File::open("foo.txt").ok().expect("Couldn’t open foo.txt");
186-
let result = f.write("whatever".as_bytes());
186+
let buf = b"whatever"; // byte string literal. buf: &[u8; 8]
187+
let result = f.write(buf);
187188
# result.unwrap(); // ignore the error
188189
```
189190

190191
Here’s the error:
191192

192193
```text
193194
error: type `std::fs::File` does not implement any method in scope named `write`
194-
195-
let result = f.write("whatever".as_bytes());
196-
^~~~~~~~~~~~~~~~~~~~~~~~~~~~
195+
let result = f.write(buf);
196+
^~~~~~~~~~
197197
```
198198

199199
We need to `use` the `Write` trait first:
@@ -202,7 +202,8 @@ We need to `use` the `Write` trait first:
202202
use std::io::Write;
203203
204204
let mut f = std::fs::File::open("foo.txt").ok().expect("Couldn’t open foo.txt");
205-
let result = f.write("whatever".as_bytes());
205+
let buf = b"whatever";
206+
let result = f.write(buf);
206207
# result.unwrap(); // ignore the error
207208
```
208209

0 commit comments

Comments
 (0)