Is there a way to create an alias for a specific FnMut?

I want to work with FnMut(&[f32]) -> f32, so as not to copy / paste the full signature, I want to enter some kind of alias, but

type Boo = FnMut(&[f32]) -> f32;

fn f<F: Boo>(mut f: F) {}

Causes a compiler error:

error[E0404]: expected trait, found type alias `Boo`
 --> src/main.rs:3:13
  |
3 |     fn f<F: Boo>(mut f: F) {}
  |             ^^^ type aliases cannot be used for traits

Then I tried:

trait Boo: FnMut(&[f32]) -> f32 {}

fn f<F: Boo>(mut f: F) {}

it compiled, but if I try to use Booinstead of the property elsewhere:

trait Boo: FnMut(&[f32]) -> f32 {}

struct X(Vec<Box<Boo>>);

I get:

error[E0191]: the value of the associated type `Output` (from the trait `std::ops::FnOnce`) must be specified
 --> src/main.rs:3:18
  |
3 | struct X(Vec<Box<Boo>>);
  |                  ^^^ missing associated type `Output` value

Is there a way to create a specific alias FnMutthat I can use instead FnMut(&[f32]) -> f32?

+5
source share
1 answer

. , RFC . , , RFC - .

, Boo FnMut, Output. , , , Output :

struct X(Vec<Box<Boo<Output = f32>>>);

, , . , Output -> f32, .

Boo FnMut(&[f32]), , Boo. . FnMut(&[f32]) -> f32, :

impl <F> Boo for F where F: FnMut(&[f32]) -> f32 {}

Boo FnMut(&[f32]) -> f32 ( ), FnMut(&[f32]) -> f32 Boo ( ).

+6

Source: https://habr.com/ru/post/1678124/


All Articles