# Conditional Compilation if unoptimized

**URL:** <https://users.rust-lang.org/t/conditional-compilation-if-unoptimized/26740>\
**Category:** help\
**Created:** [March 29, 2019, 10:32am UTC](https://users.rust-lang.org/t/conditional-compilation-if-unoptimized/26740 "2019-03-29T10:32:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kurdy](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/kurdy/32/9976_2.png) [@kurdy](https://users.rust-lang.org/u/kurdy)\
**Post date:** [March 29, 2019, 10:32am UTC](https://users.rust-lang.org/t/conditional-compilation-if-unoptimized/26740/1 "2019-03-29T10:32:51Z")

</div>

Hi,

Is there a **cfg** attribute or macro to compile a code section if we are unoptimized, in other words, exclude it if we are in release.

I'm thinking about an ideas like:

```rust
if cfg!(unoptimized) {
   println!("Configuration:{:?}",config);
}

```

Thanks

---

<div class="post-metadata">

**Author:** ![leudz](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/leudz/32/10514_2.png) [@leudz](https://users.rust-lang.org/u/leudz)\
**Post date:** [March 29, 2019, 10:52am UTC](https://users.rust-lang.org/t/conditional-compilation-if-unoptimized/26740/2 "2019-03-29T10:52:52Z")

</div>

Hi, you can use cfg!(debug\_assertions), it's what debug\_assert!() uses. I don't know if there is a more general flag.

---

<div class="post-metadata">

**Author:** ![kornel](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/kornel/32/4823_2.png) [@kornel](https://users.rust-lang.org/u/kornel)\
**Post date:** [March 29, 2019, 12:09pm UTC](https://users.rust-lang.org/t/conditional-compilation-if-unoptimized/26740/3 "2019-03-29T12:09:21Z")

</div>

There's no cfg for optimisations. There's one for debug assertions that is abused to mean debug builds in general.

However, for printing stuff conditionally, it's best to use the `log` crate. It has debug and trace macros that can be complied optionally.

---

<div class="post-metadata">

**Author:** ![kurdy](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/kurdy/32/9976_2.png) [@kurdy](https://users.rust-lang.org/u/kurdy)\
**Post date:** [March 30, 2019, 9:53am UTC](https://users.rust-lang.org/t/conditional-compilation-if-unoptimized/26740/4 "2019-03-30T09:53:21Z")

</div>

Thanks concerning log you are right but in that case it's at beginning before log is initialized.

---

<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:** [June 28, 2019, 9:53am UTC](https://users.rust-lang.org/t/conditional-compilation-if-unoptimized/26740/5 "2019-06-28T09:53:25Z")

</div>

Closed on June 28, 2019, 9:53am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
