Tips for wrangling generics infection?

Let's say I have a struct which is used in lots of places.

Now I want it generic over some type (don't want to box)

Suddenly I need to refactor a lot of code everywhere.. all the functions that take this struct need to declare the new generic too.

Are there IDE tools to make this easier? I'm using neovide/vim with the coc rust-analyzer plugin, but I'm fine with VSCode too.

A default is an option if sensible.

Not sure what you mean by that... let's say I had:

struct Foo {
    value: String

And now I want:

struct Foo<A: Display> {
    value: A

For method changes it's pretty easy, just update the impl, but what about all functions that accept a Foo? I now need to change it from like:

fn greet(foo:Foo) {
    println!("hello {}", foo.value)


fn greet<A: Display>(foo:Foo<A>) {
    println!("hello {}", foo.value)

This is really painful sometimes... not sure what "default is an option" means here?

You can do struct Foo<A: Display = String> { .. } if I'm not mistaken. On mobile so I can't check.

Sometimes batch text editing is easier to do with something designed to edit text in bulk. Perhaps a sed comand will be easier to write than using an IDE tool.

...says the guy that doesn't use an IDE :sunglasses:

1 Like

Box<dyn Trait> lets you use traits without involving generics. This is really your best option if the type arguments and/or the code bloat of generics are problematic.

&dyn Trait also exists if you can have the object borrowed temporarily, and works with objects on the stack. Good option for function arguments.


You want the IDE to make important decisions about the logical structure of your code? Adding the generics is not just boilerplate text. It can make an important difference for individual methods to have a generic argument instead of the impl block. Once that decision has been made actually changing the text is mostly find-and-replace.