From cc2d464ec1c4b27f4567747fae9801bfa3f9ed8f Mon Sep 17 00:00:00 2001 From: Jacob Lindahl Date: Sat, 10 Feb 2024 19:53:06 +0900 Subject: [PATCH] rpt 34 --- content/blog/rust-pro-tips-collection.md | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/content/blog/rust-pro-tips-collection.md b/content/blog/rust-pro-tips-collection.md index 5d13172..b17fc82 100644 --- a/content/blog/rust-pro-tips-collection.md +++ b/content/blog/rust-pro-tips-collection.md @@ -1,7 +1,7 @@ --- title: "Rust Pro Tips (collection)" date: 2023-04-08 -lastmod: 2024-01-28 +lastmod: 2024-02-10 description: "Level up your Rust skills." author: Jacob Lindahl twitter: sudo_build @@ -12,6 +12,25 @@ 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. +## 34. Enable optional dependency features with a feature + +[Tweet](https://twitter.com/sudo_build/status/1756269920126726455) [Toot](https://infosec.exchange/@hatchet/111906804455534802) + +Use the `?` syntax in `Cargo.toml` to activate features on optional dependencies only when those dependencies are enabled. + +```toml +[dependencies] +backend-a = { version = "1", optional = true } +backend-b = { version = "1", optional = true } + +[features] +default = ["backend-a"] +unstable = ["backend-a?/unstable", "backend-b?/unstable"] +# Enabling the "unstable" feature won't implicitly enable either backend. +``` + +[Docs](https://doc.rust-lang.org/cargo/reference/features.html#dependency-features) + ## 33. Use tuple struct initializers as function pointers [Tweet](https://twitter.com/sudo_build/status/1751597656114446377) [Toot](https://infosec.exchange/@hatchet/111833792212244273)