Skip to content

Commit cc2d464

Browse files
committed
rpt 34
1 parent 4d77282 commit cc2d464

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

content/blog/rust-pro-tips-collection.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Rust Pro Tips (collection)"
33
date: 2023-04-08
4-
lastmod: 2024-01-28
4+
lastmod: 2024-02-10
55
description: "Level up your Rust skills."
66
author: Jacob Lindahl
77
twitter: sudo_build
@@ -12,6 +12,25 @@ license:
1212

1313
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.
1414

15+
## 34. Enable optional dependency features with a feature
16+
17+
[Tweet](https://twitter.com/sudo_build/status/1756269920126726455) [Toot](https://infosec.exchange/@hatchet/111906804455534802)
18+
19+
Use the `?` syntax in `Cargo.toml` to activate features on optional dependencies only when those dependencies are enabled.
20+
21+
```toml
22+
[dependencies]
23+
backend-a = { version = "1", optional = true }
24+
backend-b = { version = "1", optional = true }
25+
26+
[features]
27+
default = ["backend-a"]
28+
unstable = ["backend-a?/unstable", "backend-b?/unstable"]
29+
# Enabling the "unstable" feature won't implicitly enable either backend.
30+
```
31+
32+
[Docs](https://doc.rust-lang.org/cargo/reference/features.html#dependency-features)
33+
1534
## 33. Use tuple struct initializers as function pointers
1635

1736
[Tweet](https://twitter.com/sudo_build/status/1751597656114446377) [Toot](https://infosec.exchange/@hatchet/111833792212244273)

0 commit comments

Comments
 (0)