Hone logo
Hone
Problems

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:

  1. A FeatureFlags struct: This struct will hold the feature flag configurations. The configuration should be a HashMap where the key is the feature flag name (a String) and the value is a boolean representing whether the feature is enabled (true) or disabled (false).

  2. An enable_feature function: This function takes the feature flag name (a String) and a mutable reference to a FeatureFlags instance as input. It sets the feature flag to true in the configuration.

  3. A disable_feature function: This function takes the feature flag name (a String) and a mutable reference to a FeatureFlags instance as input. It sets the feature flag to false in the configuration.

  4. An is_feature_enabled function: This function takes the feature flag name (a String) and a reference to a FeatureFlags instance as input. It returns true if the feature is enabled, false otherwise. If the feature flag doesn't exist in the configuration, it should return false (disabled by default).

  5. A new function: This function creates a new FeatureFlags instance 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 FeatureFlags struct should be thread-safe. While not explicitly required to implement a full mutex, consider the implications of concurrent access when designing your solution.
  • The HashMap used to store the flags should be std::collections::HashMap.
  • Performance is not a primary concern for this challenge, but avoid unnecessarily inefficient operations.

Notes

  • Consider using String instead of &str for 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.
Loading editor...
rust