Vvinhnx

rand-0-8-to-0-9

Migrate Rust rand usage from 0.8 to 0.9: thread_rng to rng and gen* method renames

rustrandmigration0.8-to-0.9randomrng
Public
1 executions
2 stars
How to Use
Run this codemod on your codebase using the following command

The easiest way to run this codemod without installing anything globally:

Documentation

rand-0-8-to-0-9

Automate the deterministic Rust rand migration from v0.8 to v0.9 for the most common API renames.

What it does

RNG constructor updates

  • rand::thread_rng() -> rand::rng()
  • thread_rng() -> rng() when imported from rand
  • use rand::thread_rng; -> use rand::rng;
  • use rand::{..., thread_rng, ...}; -> use rand::{..., rng, ...};
  • Supports aliased imports (thread_rng as foo -> rng as foo)

Method renames

  • .gen(...) -> .random(...)
  • .gen_range(...) -> .random_range(...)
  • .gen_bool(...) -> .random_bool(...)
  • .gen_ratio(...) -> .random_ratio(...)
  • Rng::gen(...) -> Rng::random(...)
  • Rng::gen_range(...) -> Rng::random_range(...)
  • Rng::gen_bool(...) -> Rng::random_bool(...)
  • Rng::gen_ratio(...) -> Rng::random_ratio(...)

Cargo.toml follow-up

  • Update rand = "0.8.x" -> rand = "0.9"
  • Update rand = { version = "0.8.x", ... } -> rand = { version = "0.9", ... }

Usage

bash

Manual follow-up

After running, you should:

  1. Review Cargo.toml updates for uncommon rand dependency formatting not covered by deterministic rewrites
  2. Run cargo check and cargo test
  3. Review call sites using custom RNG traits or methods named similarly to gen
  4. Handle deprecations outside this codemod's deterministic rename scope

Development

bash

References

License

MIT

Before

This is one example from the codemod's test cases. The codemod may handle many more cases.

Ready to contribute?

Build your own codemod and share it with the community.