Clippy lints / Performance
Clippy lint · Performance · checked with Clippy 0.1.99
clippy::sliced_string_as_bytes: calling `as_bytes` after slicing a string
What it meansChecks for string slices immediately followed by as_bytes. It is in the performance group and warns by default.
What you see
warning: calling `as_bytes` after slicing a string --> main.rs:4:1 | 4 | s[1..5].as_bytes(); | ^^^^^^^^^^^^^^^^^^ help: try: `&s.as_bytes()[1..5]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#sliced_string_as_bytes = note: `#[warn(clippy::sliced_string_as_bytes)]` on by default
Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.
Does it matter?
It involves doing an unnecessary UTF-8 alignment check which is less efficient, and can cause a panic.
Code that could be written to run faster. Warns by default.
How to fix it
Clippy does not fix this one automatically (its suggestion may change behaviour, so it only shows it). Change the code by hand:
Flagged
let s = "Lorem ipsum";
s[1..5].as_bytes();
Write this instead
let s = "Lorem ipsum";
&s.as_bytes()[1..5];
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::sliced_string_as_bytes)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
sliced_string_as_bytes = "allow"One run
cargo clippy -- -A clippy::sliced-string-as-bytesThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Clippy's own documentation
What it does
Checks for string slices immediately followed by as_bytes.
Why is this bad?
It involves doing an unnecessary UTF-8 alignment check which is less efficient, and can cause a panic.
Known problems
In some cases, the UTF-8 validation and potential panic from string slicing may be required for
the code’s correctness. If you need to ensure the slice boundaries fall on valid UTF-8 character
boundaries, the original form (s[1..5].as_bytes()) should be preferred.
Example
let s = "Lorem ipsum";
s[1..5].as_bytes();
Use instead:
let s = "Lorem ipsum";
&s.as_bytes()[1..5];
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- large_enum_variantlarge size difference between variants
- regex_creation_in_loopsChecks for regex compilation inside a loop with a literal
- replace_boxcreating a new box with default content
- result_large_errthe `Err`-variant returned from this function is very large
- slow_vector_initializationChecks slow zero-filled vector initialization
- to_string_in_format_argsChecks for ToString::to_string applied to a type that implements Display in a macro that does formatting
- unbuffered_bytescalling .bytes() is very inefficient when data is not in memory
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::large_enum_variantlarge size difference between variantsRead it next →Did this answer your question?