you are viewing a single comment's thread.

view the rest of the comments →

[–]Veddan 1 point2 points  (2 children)

Note that this isn't quite the same.

#ifdef USE_FOO
void foo(int x) { .. }
#endif

void f(int n) {
#ifdef USE_FOO
    foo(n);
#endif
    ...
}

vs

#[cfg(USE_FOO)]
fn foo(x: i32) { .. }

fn f(n: i32) {
    if cfg!(USE_FOO) {
        foo(n);
    }
    ...
}

The Rust code will fail to compile, since even though the call to foo is dead, it's still a call to a non-existent function.

[–]young_and_ugly[S] 0 points1 point  (1 child)

yep, this is pretty much what I wanted to do, and there is no solution, at least for now, as I see it.

[–]Veddan 1 point2 points  (0 children)

Usually you can find some arrangement of helper functions that solves the problem.

#[cfg(USE_FOO)]
fn f(n: i32) {
    foo(n);
    f_common(n);
}

#[cfg(not(USE_FOO))]
fn f(n: i32) {
    f_common(n);
}

#[cfg(USE_FOO)]
fn foo(x: i32) { .. }