Rust

Rust

By DeepSource

Found manual implementation of .split_once(..) RS-W1066

Anti-pattern

splitn(2, ..) is more written more concisely with split_once.

The key difference between the two methods are their return types:

  • "a string".splitn(2, ..) returns an Iterator<Item = &str>
  • "a string".split_once(..) returns an Option<(&str, &str)>

Examples

Bad practice

let mut x = "test=value=items".splitn(2, '=');
if let (Some(x1), Some(x2)) = (x.next(), x.next()) {
    // ..
}

Recommended

if let Some((x1, x2)) = "test=value=items".split_once('=') {
    // ..
}