How can I use internal mutability with generic type in Rust?
问题 I would like to design a struct in Rust that can be constructed with an object that implements the Digest trait, and abstract the behavior of the hash behind a method. Here's a simple example that doesn't compile: use digest::Digest; struct Crypto<D: Digest> { digest: D, } impl<D> Crypto<D> where D: Digest, { pub fn hash(&self, data: &[u8]) -> Vec<u8> { self.digest.chain(&data).finalize_reset().to_vec() } } This fails to compile because self is immutably borrowed in the method signature, so