Skip to content

Flip order of s[a..b].as_bytes() #10981

Closed
@A-Walrus

Description

@A-Walrus

What it does

Suggests replacing slicing a str followed by as_bytes with the opposite order

s[a..b].as_bytes()

with

&s.as_bytes()[a..b]

Advantage

  • Removes unnecessary utf8 indexing check
  • won't panic if a and b are not on utf8 boundaries

Drawbacks

Technically possible someone relied on it panicking, but find that unlikely.

Example

let s = "Hello World";
let bytes = s[1..3].as_bytes();

Could be written as:

let s = "Hello World";
let bytes = &s.as_bytes()[1..3];

Metadata

Metadata

Assignees

Labels

A-lintArea: New lints

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions