Why todo!() not working?

i tried to use todo!() to compile when i didn't complete a function, but todo!() do not allow me to compile

fn todo_not_working() -> impl Fn() {
    todo!()
}

Here is some more discussion on the subject of the never type not coercing to something that satisfies an RPIT:

3 Likes

could u tell me how can i compile without implement this code?

You could return a dummy value after the todo! statement. This will still panic if you ever call the function but you also satisfy the return type:

#[allow(unreachable_code)]
fn todo_not_working() -> impl Fn() {
    todo!();    
    || {}
}
2 Likes

In the case you cannot easily come up with an expression that implements the trait you can alternatively cast ! to any type:

fn todo_not_working() -> impl Fn() {
    todo!() as fn()
}

or with a macro to get rid of the unreachable code warning

macro_rules! typed_todo {
    ($t:ty) => (#[allow(unreachable_code)]{
        todo!() as $t
    })
}

pub fn todo_not_working() -> impl Fn() {
    typed_todo!(fn())
}
7 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.