# Conditional compilation

**URL:** <https://users.rust-lang.org/t/conditional-compilation/49239>\
**Category:** help\
**Created:** [September 24, 2020, 9:21am UTC](https://users.rust-lang.org/t/conditional-compilation/49239 "2020-09-24T09:21:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![BrunoVDR](https://avatars.discourse-cdn.com/v4/letter/b/74df32/32.png) [@BrunoVDR](https://users.rust-lang.org/u/BrunoVDR)\
**Post date:** [September 24, 2020, 9:21am UTC](https://users.rust-lang.org/t/conditional-compilation/49239/1 "2020-09-24T09:21:53Z")

</div>

Hi,

I'm looking for a way to compile some code in my debug build, but not in my release build. More specifically I would like **a part** of a function not to be compiled on release. In other word, something similar to C/C++ assert with NDEBUG define. I'm aware of conditionnal compilation

```rust
#[cfg(target_os = "macos")]

```

but from what I understand, it's function wise only (I don't want duplicated code), and it's not possible to create our own options.

Any help is appreciated !

---

<div class="post-metadata">

**Author:** ![sanxiyn](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/sanxiyn/32/24284_2.png) [@sanxiyn](https://users.rust-lang.org/u/sanxiyn)\
**Post date:** [September 24, 2020, 9:25am UTC](https://users.rust-lang.org/t/conditional-compilation/49239/2 "2020-09-24T09:25:56Z")

</div>

`#[cfg]` do work on statements and blocks.

---

<div class="post-metadata">

**Author:** ![BrunoVDR](https://avatars.discourse-cdn.com/v4/letter/b/74df32/32.png) [@BrunoVDR](https://users.rust-lang.org/u/BrunoVDR)\
**Post date:** [September 24, 2020, 10:05am UTC](https://users.rust-lang.org/t/conditional-compilation/49239/3 "2020-09-24T10:05:42Z")

</div>

Do you mean that :

```rust
            #[cfg(target_os = "unix")]
            {
                println!("Unix");
            }

```

would work inside a function ? And if true, is there an attribute to get the build type like : build\_type = "debug" or others ? I could not find one [here](https://doc.rust-lang.org/reference/attributes.html#built-in-attributes-index).

---

<div class="post-metadata">

**Author:** ![bjorn3](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/bjorn3/32/5255_2.png) [@bjorn3](https://users.rust-lang.org/u/bjorn3)\
**Post date:** [September 24, 2020, 10:07am UTC](https://users.rust-lang.org/t/conditional-compilation/49239/4 "2020-09-24T10:07:58Z")

</div>

You can use

```rust
#[cfg(debug_assertions)]
{
    println!("debug mode");
}

```

---

<div class="post-metadata">

**Author:** ![BrunoVDR](https://avatars.discourse-cdn.com/v4/letter/b/74df32/32.png) [@BrunoVDR](https://users.rust-lang.org/u/BrunoVDR)\
**Post date:** [September 24, 2020, 10:22am UTC](https://users.rust-lang.org/t/conditional-compilation/49239/5 "2020-09-24T10:22:27Z")

</div>

That's what I was looking for, many thanks !

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [December 23, 2020, 10:22am UTC](https://users.rust-lang.org/t/conditional-compilation/49239/6 "2020-12-23T10:22:28Z")

</div>

Closed on December 23, 2020, 10:22am UTC

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.
