I need to use func have type F inside function

I need to use func variable having type F inside function.
As far as it looks like my understanding of this problems wrong here is full task

trait IntFloatTrait: Sized { }
impl<T> IntFloatTrait for T { }
fn register_print<F: 'static + FuncTrait<T, i64>,T: 'static + IntFloatTrait>(func: F) {
    let input_value = 10;
    let result = func(input_value);

    println!("Input: {}, Output: {}", input_value, result);
}
// do not touch anything below this line
//////////////////////////////////////////
trait FuncTrait<T, U> {
}
impl<T, U, F> FuncTrait<T, U> for F where F: Fn(T) -> U {
}
fn add_two_integers(a: i64) -> i64 {
    a + 2
}
fn register<F: 'static + FuncTrait<T, i64>,T: 'static + IntFloatTrait>(func: F) {
    register_print(func)
}
fn main() {
    register(add_two_integers);
}


Original post was:

// do not change function definition but change body
fn register<F: 'static + FuncTrait<i64, i64>>(func: F) {
// start changes
    let input_value = 10;
    let result = func(input_value);
// end changes
    println!("Input: {}, Output: {}", input_value, result);
}
// do not change anything below this line
trait FuncTrait<T, U> {
}

impl<T, U, F> FuncTrait<T, U> for F where F: Fn(T) -> U {
}

fn add_two_integers(a: i64) -> i64 {
    a + 2
}

fn main() {
    register(add_two_integers);
}

and I have an compilation error

error[E0618]: expected function, found `F`
  --> src/main.rs:13:18
   |
11 | fn register<F: 'static + FuncTrait<i64, i64>>(func: F) {
   |                                               ---- `func` has type `F`
12 |     let input_value = 10;
13 |     let result = func(input_value);
   |                  ^^^^-------------
   |                  |
   |                  call expression requires function

In your example, the compiler can't call the Fn, because your FuncTrait doesn't have anything to do with it.

You can do either of these:

  1. Use Fn directly
  2. Add a function to your custom trait that calls through to the Fn implementation.
  3. Change your trait to FuncTrait<T, U>: Fn(T) -> U {}

2 and 3 are only useful if you want to add additional methods to FuncTrait

1 Like

problem is i can change only body of register function itself

Then the problem is unsolvable. The way FuncTrait is written, it does not allow calling the function.

5 Likes

Can you change register's signature?:

fn register<F: 'static + FuncTrait<i64, i64> + Fn(i64) -> i64>(func: F) 

actually not rule is no changes to function definition, stuck for this for 8 hours already )))

Either you have misunderstood the problem, or whoever wrote it does not understand Rust (or made a mistake).

Is this problem written somewhere we can read the original? Perhaps there is a missing detail.

2 Likes

Someone somewhere seems to be creating homework with bizarre restrictions, throwing people off. Or maybe there's a human language barrier understanding the problem descriptions.

actually it is a part of big function, for simplicity sake I have removed all unneccary code

Also I know few peoples already solved this part, but i dont know how )))

What are you allowed to change in this programming assignment? As far as I understand, you can't change anything after the definition of register, and you can't change the body of register itself, so the only thing you're allowed to change is the signature of register.

If you add a bound on F in the signature, as in F: Fn(i64) -> i64 + 'static + ..., it compiles. Still a bizarre homework problem, but solvable.

1 Like

Sorry, my bad we can change only body of register function, but not the definition

I can imagine a C++ teacher somewhere:

Recreate std::move without using a forwarding reference.

2 Likes

If by "definition" you mean the signature, then the problem is strictly unsolvable. Often that means you misunderstood the problem, so more context would help.

1 Like

I know for sure it is possible, few peoples already did it

Maybe you are right and it is my missunderstanding full task is:

trait IntFloatTrait: Sized { }
impl<T> IntFloatTrait for T { }
fn register_print<F: 'static + FuncTrait<T, i64>,T: 'static + IntFloatTrait>(func: F) {
    let input_value = 10;
    let result = func(input_value);

    println!("Input: {}, Output: {}", input_value, result);
}
// do not touch anything below this line
//////////////////////////////////////////
trait FuncTrait<T, U> {
}
impl<T, U, F> FuncTrait<T, U> for F where F: Fn(T) -> U {
}
fn add_two_integers(a: i64) -> i64 {
    a + 2
}
fn register<F: 'static + FuncTrait<T, i64>,T: 'static + IntFloatTrait>(func: F) {
    register_print(func)
}
fn main() {
    register(add_two_integers);
}
1 Like

And what are you expected to do? Just make the code compile? Is it supposed to do something in particular?

Just need to be compiled

As written, this can't be done.

2 Likes

Looks like teacher did a typo in definition )))