This repository was archived by the owner on May 28, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +21
-1
lines changed Expand file tree Collapse file tree 1 file changed +21
-1
lines changed Original file line number Diff line number Diff line change @@ -69,7 +69,27 @@ declare_clippy_lint! {
69
69
/// **Why is this bad?** Byte string literals (e.g., `b"foo"`) can be used
70
70
/// instead. They are shorter but less discoverable than `as_bytes()`.
71
71
///
72
- /// **Known Problems:** None.
72
+ /// **Known Problems:**
73
+ /// `"str".as_bytes()` and the suggested replacement of `b"str"` are not
74
+ /// equivalent because they have different types. The former is `&[u8]`
75
+ /// while the latter is `&[u8; 3]`. That means in general they will have a
76
+ /// different set of methods and different trait implementations.
77
+ ///
78
+ /// ```rust
79
+ /// fn f(v: Vec<u8>) {}
80
+ ///
81
+ /// f("...".as_bytes().to_owned()); // works
82
+ /// f(b"...".to_owned()); // does not work, because arg is [u8; 3] not Vec<u8>
83
+ ///
84
+ /// fn g(r: impl std::io::Read) {}
85
+ ///
86
+ /// g("...".as_bytes()); // works
87
+ /// g(b"..."); // does not work
88
+ /// ```
89
+ ///
90
+ /// The actual equivalent of `"str".as_bytes()` with the same type is not
91
+ /// `b"str"` but `&b"str"[..]`, which is a great deal of punctuation and not
92
+ /// more readable than a function call.
73
93
///
74
94
/// **Example:**
75
95
/// ```rust
You can’t perform that action at this time.
0 commit comments