'How do I write a function that can take in an array of two possible sizes? [duplicate]
I have two functions that do the same thing; the only difference is that they take in an array of different sizes. How can I refactor this code such that I don't get duplicate code?
fn foo(arr: &[[u8; 21]; 21]) -> [[u8; 21]; 21] {
// some operations that need to iterate through arr
}
fn bar(arr: &[[u8; 25]; 25]) -> [[u8; 25]; 25] {
// identical operations as foo
}
I tried using generics:
fn foo<T>(arr: &T) -> T {
// some operations
}
This doesn't work because Rust has no idea that arr is an array, and I'm not able to call things like arr.len().
Another idea is to use a Vec instead, but I worry it comes at a cost of performance and memory.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
