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
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.
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.
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.