Automatically replace all try!() with `?`

If you've got code that still uses the try!() macro, you can quickly and accurately upgrade it to the ? operator.

  1. Create file with:

    fn rule1<T,E,X: From<E>>(r: Result<T,E>) -> Result<T,X> {
        replace!(r#try!(r) => r?);
  2. Then run:

    rustup update
    cargo +nightly install rerast
    cargo +nightly rerast --force

and poof! the macro will be gone. Rerast operates on the AST and fixes priorities of operations if necessary, so it's much smarter and more correct than textual find'n'replace.


Very cool! :+1:

Very cool indeed, it just surprised me by being authored by Google. I always thought they had restrictions on which languages can be used by Googlers.

This tool, once extended with support for other syntactic forms (e.g. statements) and ‘find definition’ and ‘find usages’ functionality, could go a long way towards proper refactoring support couldn’t it?

Not for side-projects - this (as well as several others written by Google employees) is not an official Google product.

1 Like is another project written by a google employee in rust


The next step would be to write a GitHub bot for this :wink:



There is another way for those project used rustfmt suggested by Jonas:

Use rustfmt and enable the use_try_shorthand config option.

1 Like