# How to implement with generic\_const\_expr?

**URL:** <https://users.rust-lang.org/t/how-to-implement-with-generic-const-expr/136906>\
**Category:** help\
**Created:** [December 13, 2025, 2:27am UTC](https://users.rust-lang.org/t/how-to-implement-with-generic-const-expr/136906 "2025-12-13T02:27:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mono-cyte](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mono-cyte/32/50416_2.png) [@mono-cyte](https://users.rust-lang.org/u/mono-cyte)\
**Post date:** [December 13, 2025, 2:27am UTC](https://users.rust-lang.org/t/how-to-implement-with-generic-const-expr/136906/1 "2025-12-13T02:27:11Z")

</div>

Cannot use generic const parameters in arithmetic operations

```rust
impl<T, const R: usize, const C: usize> Matrix<T, R, C>
where
    T: Default + Copy,
{

    pub fn minor_matrix(&self, row: usize, col: usize) -> Matrix<T, { C - 1 }, { R - 1 }> {
        let mut result = Matrix::default();

        let mut new_r = 0;
        for r in 0..R {
            if r == row {
                continue;
            }
            let mut new_c = 0;
            for c in 0..C {
                if c == col {
                    continue;
                }
                result[new_r][new_c] = self[r][c];
                new_c += 1;
            }
            new_r += 1;
        }
        result
    }
}

```

---

<div class="post-metadata">

**Author:** ![robofinch](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/robofinch/32/49287_2.png) [@robofinch](https://users.rust-lang.org/u/robofinch)\
**Post date:** [December 13, 2025, 4:07am UTC](https://users.rust-lang.org/t/how-to-implement-with-generic-const-expr/136906/2 "2025-12-13T04:07:56Z")

</div>

It works (or seems to work) with the nightly-only `generic_const_exprs` feature: [Rust Playground](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=71fb51e1a8e226874e8d00cf0da8ce68).

Though do note that there’s a warning: “the feature `generic_const_exprs` is incomplete and may not be safe to use and/or cause compiler crashes”.

I think there’s 0 chance of something like that using const generics working on stable. There are other ways of encoding numbers in the type system, though.

---

<div class="post-metadata">

**Author:** ![mono-cyte](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mono-cyte/32/50416_2.png) [@mono-cyte](https://users.rust-lang.org/u/mono-cyte)\
**Post date:** [December 13, 2025, 7:27am UTC](https://users.rust-lang.org/t/how-to-implement-with-generic-const-expr/136906/3 "2025-12-13T07:27:00Z")

</div>

`N-1` as generic param cannot work with recursive `determinant`

```rust
impl<T, const N: usize> Matrix<T, N, N>
where
    T: Default + Copy + Sub<Output = T> + Mul<Output = T> + AddAssign + Neg<Output = T>,
{
    const fn determinant(&self) -> T {
        if N == 1 {
            self[0][0]
        } else if N == 2 {
            self[0][0] * self[1][1] - self[1][0] * self[0][1]
        } else {
            let mut det = T::default();
            for j in 0..N {
                let minor = self.minor_matrix(0, j);
                if j % 2 == 0 {
                    det += self[0][j] * minor.determinant();
                } else {
                    det += -(self[0][j] * minor.determinant());
                } ;
            }
            det
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![robofinch](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/robofinch/32/49287_2.png) [@robofinch](https://users.rust-lang.org/u/robofinch)\
**Post date:** [December 13, 2025, 2:06pm UTC](https://users.rust-lang.org/t/how-to-implement-with-generic-const-expr/136906/4 "2025-12-13T14:06:25Z")

</div>

Remember to account for `N==0`; integer underflow might be causing a compile-time error.

Also, a `match` would be more idiomatic than `if / else if` on the same variable.

---

<div class="post-metadata">

**Author:** ![mono-cyte](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mono-cyte/32/50416_2.png) [@mono-cyte](https://users.rust-lang.org/u/mono-cyte)\
**Post date:** [December 15, 2025, 4:50am UTC](https://users.rust-lang.org/t/how-to-implement-with-generic-const-expr/136906/5 "2025-12-15T04:50:46Z")

</div>

```rust

impl<T, const R: usize, const C: usize> Matrix<T, R, C>
where
    T: Default + Copy,
    [(); R - 1]:,
    [(); C - 1]:,
{
    pub fn minor_matrix(&self, row: usize, col: usize) -> Matrix<T, { R - 1 }, { C - 1 }>
    where
    {
        let mut result = Matrix::default();

        let mut new_r = 0;
        for r in 0..R {
            if r == row {
                continue;
            }
            let mut new_c = 0;
            for c in 0..C {
                if c == col {
                    continue;
                }
                result[new_r][new_c] = self[r][c];
                new_c += 1;
            }
            new_r += 1;
        }
        result
    }
}

impl<T, const N: usize> Matrix<T, N, N>
where
    T: Default + Copy + Sub<Output = T> + Mul<Output = T> + AddAssign + Neg<Output = T>,
    [(); N - 1]:,
{
    fn determinant(&self) -> T {
        match N {
            1 => self[0][0],
            2 => self[0][0] * self[1][1] - self[1][0] * self[0][1],
            _ => {
                let mut det = T::default();
                for j in 0..N {
                    let minor = self.minor_matrix(0, j);
                    if j % 2 == 0 {
                        det += self[0][j] * minor.determinant();
                    } else {
                        det += -(self[0][j] * minor.determinant());
                    };
                }
                det
            }
        }
    }
}

```

However, I should have already constrained N != 0.  
The Rust compiler seems unable to infer generic recursion in ` minor.determinent()` for `Matrix<T,{R-1},{C-1}>`  
I'm exploring Rust's generics, so I'm not using LU.

While in cpp, I can easily do like:

```cpp
template <class T, size_t R, size_t C>
auto Matrix<T, R, C>::det() const
  requires(R == C && std::is_arithmetic_v<T>)
{
  if constexpr (R == 1) {
    return data[0][0];
  } else if constexpr (R == 2) {
    return data[0][0] * data[1][1] - data[0][1] * data[1][0];
  } else {
    T determinant = 0;
    for (size_t c = 0; c < C; ++c) {
      T cofactor = ((c % 2 == 0) ? 1 : -1) * data[0][c];
      auto minor = minor_matrix(0, c);
      determinant += cofactor * minor.det();
    }
    return determinant;
  }
}

```

---

<div class="post-metadata">

**Author:** ![robofinch](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/robofinch/32/49287_2.png) [@robofinch](https://users.rust-lang.org/u/robofinch)\
**Post date:** [December 15, 2025, 5:22am UTC](https://users.rust-lang.org/t/how-to-implement-with-generic-const-expr/136906/6 "2025-12-15T05:22:46Z")

</div>

Ah, I guess recursion won't work out very well. Rust tries to avoid post-monomorphization errors in its type/trait system (therefore, the `minor_matrix` method literally does not exist on a `0x0` matrix, and the trait bounds enforce that fact even before monomorphization), and I should've known that the type system doesn't care whether something is reachable.

```rust
if false {
    let x = 1_i32.non_existent_method();
}

```

will throw an error, and for the same reason, it doesn't matter that the problematic function call is unreachable in the `N==0` case. I don't think Rust has an equivalent of constexpr if statements... though there might be something similar with specialization? I briefly tried out implementing recursion and a base case with the `min_specialization` feature, but it indicated that the bound `[(); N - 1]:,` cannot be specialized on for whatever reason.

(There is [`typenum`](https://docs.rs/typenum/latest/typenum/) if you haven't heard of it, but feel free to have fun exploring Rust's generics and type system.)

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [March 15, 2026, 5:23am UTC](https://users.rust-lang.org/t/how-to-implement-with-generic-const-expr/136906/7 "2026-03-15T05:23:20Z")

</div>

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
