Implementing methods by "use"

A few times I've been bitten by this:

use something::Foo;

fn do_something(blah: Foo) {
  blah.cat();
  blah.cow();
}

And then the compiler croaks on blah.cow(), stating it doesn't exist in Foo even through I see that it should according to the docs .. well, until I see that there's also a FooExt, and when I change it to use something::{Foo, FooExt} then blah.cow() comes into existence.

How is this sort of thing accomplished? I most recently encountered this when I had forgotten to add BufMut to use bytes::{BytesMut, BufMut}, but it has happened a few other times with *Ext imports.

Also, why is this pattern used? Is the idea that a struct could only implement the part of an implementation that's needed? I.e.:

use foo::File; // File object
use foo::FileRead; // read methods on File
use foo::FileWrite; // write methods on File

See this recent thread (link is to what seems most relevant to your post.)

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.