[Pre-RFC] Varying Generic parameters with features

I recently had a discussion about variable generic parameters in impl<...> blocks over in the users forum.

I was looking for a way to make the following code work

// This syntax is totally fine and working right now
struct Container<T, #[cfg(feature = "some_feature")] S> {
    element: T,
    #[cfg(feature = "some_feature")]
    second_element: S,

// THIS is the part which is not working
impl<T, #[cfg(feature = "some_feature")] S> Container<T, #[cfg(feature = "some_feature")] S> {
    fn get_element(&self) -> T {

    #[cfg(feature = "some_feature")]
    fn get_second_element(&self) -> S {

While some workarounds were proposed, I could not find a satisfying approach without drawbacks. In my opinion, the language should support statements such as impl<#[cfg(feature = "some_feature")] T> since they are already supported when specifying structs (see code above).

I would love to hear some feedback. Unfortunately I have never had the time to get to know how the compiler works. Thus I would love to hear some feedback before I try to come up with an idea for a possible implementation.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.