Bring builtins methods into scope

I use u8 methods many times without instance, like:

[b'a', b'1', b'2', b'3'].iter().map(u8::is_ascii_digit)

(Edit: This is what I'm doing today, I'm looking for the following paragraph.)

I would like to bring the methods I use into scope, e.g.:

use u8::is_ascii_digit;

But the above code fails with:

error[E0432]: unresolved import `u8`
 --> src\liblexer\hot_path\
2 | use u8::is_ascii_digit;
  |     ^^ help: a similar path exists: `std::u8`

It will also be nice if I could alias them.

Is it possible to achieve with Rust?

No, bringing trait or inherent methods into scope like that isn't currently supported with use. (This often comes up as a wish for things like use Default::default;.)

You can, of course, wrap it in a function named is_ascii_digit, but personally I'd just keep doing what you already are (or just use |x| x.is_ascii_digit()).

1 Like