Feature Flag Management in Rust
Feature flags (also known as feature toggles) are a powerful technique for enabling or disabling features in your application without deploying new code. This challenge asks you to implement a simple feature flag management system in Rust, allowing you to dynamically control feature availability based on a configuration. This is useful for A/B testing, gradual rollouts, and emergency feature disabling.
Problem Description
You need to create a module named feature_flags that provides functionality for managing feature flags. The module should include:
-
A
FeatureFlagsstruct: This struct will hold the feature flag configurations. The configuration should be aHashMapwhere the key is the feature flag name (aString) and the value is a boolean representing whether the feature is enabled (true) or disabled (false). -
An
enable_featurefunction: This function takes the feature flag name (aString) and a mutable reference to aFeatureFlagsinstance as input. It sets the feature flag totruein the configuration. -
A
disable_featurefunction: This function takes the feature flag name (aString) and a mutable reference to aFeatureFlagsinstance as input. It sets the feature flag tofalsein the configuration. -
An
is_feature_enabledfunction: This function takes the feature flag name (aString) and a reference to aFeatureFlagsinstance as input. It returnstrueif the feature is enabled,falseotherwise. If the feature flag doesn't exist in the configuration, it should returnfalse(disabled by default). -
A
newfunction: This function creates a newFeatureFlagsinstance with an empty configuration.
Examples
Example 1:
Input:
- feature_flags::new()
- feature_flags::enable_feature("new_ui".to_string(), &mut flags)
- feature_flags::is_feature_enabled("new_ui".to_string(), &flags)
Output:
true
Explanation: A new FeatureFlags instance is created. The "new_ui" feature is enabled. Checking if "new_ui" is enabled returns true.
Example 2:
Input:
- feature_flags::new()
- feature_flags::enable_feature("dark_mode".to_string(), &mut flags)
- feature_flags::disable_feature("dark_mode".to_string(), &mut flags)
- feature_flags::is_feature_enabled("dark_mode".to_string(), &flags)
Output:
false
Explanation: A new FeatureFlags instance is created. "dark_mode" is enabled, then disabled. Checking if "dark_mode" is enabled returns false.
Example 3: (Edge Case - Feature Not Found)
Input:
- feature_flags::new()
- feature_flags::is_feature_enabled("unknown_feature".to_string(), &flags)
Output:
false
Explanation: A new FeatureFlags instance is created. Checking for a feature that doesn't exist returns false (disabled by default).
Constraints
- The feature flag names will be strings.
- The
FeatureFlagsstruct should be thread-safe. While not explicitly required to implement a full mutex, consider the implications of concurrent access when designing your solution. - The
HashMapused to store the flags should bestd::collections::HashMap. - Performance is not a primary concern for this challenge, but avoid unnecessarily inefficient operations.
Notes
- Consider using
Stringinstead of&strfor feature flag names to allow for more flexibility. - Think about how you would handle errors or invalid input (e.g., attempting to enable a feature with an empty name). For this challenge, you can assume valid input.
- The goal is to create a clean, well-structured, and easy-to-use feature flag management system. Focus on clarity and correctness.
- You are not required to persist the feature flag configurations to disk. The flags should be held in memory.