@@ -2135,9 +2135,16 @@ std = []
2135
2135
2136
2136
#### Possibly-breaking: removing an optional dependency {#cargo-remove-opt-dep}
2137
2137
2138
- Removing an optional dependency can break a project using your library because
2138
+ Removing an [ optional dependency] [ opt-dep ] can break a project using your library because
2139
2139
another project may be enabling that dependency via [ Cargo features] .
2140
2140
2141
+ When there is an optional dependency, cargo implicitly defines a feature of
2142
+ the same name to provide a mechanism to enable the dependency and to check
2143
+ when it is enabled. This problem can be avoided by using the ` dep: ` syntax in
2144
+ the ` [features] ` table, which disables this implicit feature. Using ` dep: `
2145
+ makes it possible to hide the existence of optional dependencies under more
2146
+ semantically-relevant names which can be more safely modified.
2147
+
2141
2148
``` toml
2142
2149
# Breaking change example
2143
2150
@@ -2152,7 +2159,33 @@ curl = { version = "0.4.31", optional = true }
2152
2159
# ..curl removed
2153
2160
```
2154
2161
2162
+ ``` toml
2163
+ # MINOR CHANGE
2164
+ #
2165
+ # This example shows how to avoid breaking changes with optional dependencies.
2166
+
2167
+ # ##########################################################
2168
+ # Before
2169
+ [dependencies ]
2170
+ curl = { version = " 0.4.31" , optional = true }
2171
+
2172
+ [features ]
2173
+ networking = [" dep:curl" ]
2174
+
2175
+ # ##########################################################
2176
+ # After
2177
+ [dependencies ]
2178
+ # Here, one optional dependency was replaced with another.
2179
+ hyper = { version = " 0.14.27" , optional = true }
2180
+
2181
+ [features ]
2182
+ networking = [" dep:hyper" ]
2183
+ ```
2184
+
2155
2185
Mitigation strategies:
2186
+ * Use the ` dep: ` syntax in the ` [features] ` table to avoid exposing optional
2187
+ dependencies in the first place. See [ optional dependencies] [ opt-dep ] for
2188
+ more information.
2156
2189
* Clearly document your features. If the optional dependency is not included
2157
2190
in the documented list of features, then you may decide to consider it safe
2158
2191
to change undocumented entries.
@@ -2166,6 +2199,8 @@ Mitigation strategies:
2166
2199
optional dependencies necessary to implement "networking". Then document the
2167
2200
"networking" feature.
2168
2201
2202
+ [ opt-dep ] : features.md#optional-dependencies
2203
+
2169
2204
#### Minor: changing dependency features {#cargo-change-dep-feature}
2170
2205
2171
2206
It is usually safe to change the features on a dependency, as long as the
0 commit comments