Skip to content

Commit

Permalink
feat: rpt 35 - compile_error!()
Browse files Browse the repository at this point in the history
  • Loading branch information
encody committed May 17, 2024
1 parent 3bae11c commit 22b798d
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion content/blog/rust-pro-tips-collection.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "Rust Pro Tips (collection)"
date: 2023-04-08
lastmod: 2024-04-26
lastmod: 2024-05-17
description: "Level up your Rust skills."
author: Jacob Lindahl
twitter: sudo_build
Expand All @@ -12,6 +12,47 @@ license:

This is a collection of Rust "pro tips" that I've collected, most of which have been [posted on Twitter](https://twitter.com/search?q=%23RustProTip%20%40sudo_build&src=typed_query&f=top). I'll keep updating this post as I write more. Tips are ordered in reverse chronological order, with the most recent ones at the top.

## 35. Force compilation failure

The `compile_error!(...)` macro forces compilation failure. This can be useful to indicate unsupported feature flag combinations or invalid macro arguments.

```rust
compile_error!("Boo!");

fn main() {
println!("Hello, world!");
}
```

Compiler output:

```txt
Compiling playground v0.0.1 (/playground)
error: Boo!
--> src/main.rs:1:1
|
1 | compile_error!("Boo!");
| ^^^^^^^^^^^^^^^^^^^^^^
error: could not compile `playground` (bin "playground") due to 1 previous error
```

[Playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=0f32c3705ed84087f197e860087214cc) \
[Docs](https://doc.rust-lang.org/std/macro.compile_error.html)

---

**Bonus tip #1**: Emit compiler _warnings_ using [`build.rs`](https://doc.rust-lang.org/cargo/reference/build-scripts.html):

```rust
fn main() {
#[cfg(feature = "funky-time")]
println!("cargo::warning=Funky mode is enabled!");
}
```

**Bonus tip #2**: Emit structured diagnostics from proc macros using [the nightly `Diagnostic` API](https://doc.rust-lang.org/proc_macro/struct.Diagnostic.html). ([tracking issue #54140](https://github.com/rust-lang/rust/issues/54140))

## 34. Enable optional dependency features with a feature

[Tweet](https://twitter.com/sudo_build/status/1756269920126726455) [Toot](https://infosec.exchange/@hatchet/111906804455534802)
Expand Down

0 comments on commit 22b798d

Please sign in to comment.