DEV Community

loading...

Discussion on: Why I don't believe in pure functional programming anymore

maan2003 profile image
Maan2003 • Edited
pub struct Immut<T>(T);
impl<T> std::ops::Deref for Immut<T> {
  type Target = T;
  fn deref(&self) -> &T { &self.0 }
}
pub fn immut<T>(x: T) -> Immut<T> {
  Immut(x)
}

this guarantees that Immut can't be mutated from outside this module(rust has module level privacy).

Ownership in Rust is pretty much the same thing as having variables belong to a class in object oriented languages, from what I understood. As soon as you modified a variable, everything you ever gave this variable to now implicitly has this modified version. This is very different from the functional programming approach where you explicitly have to give the modified variant of the collection to every place you want to see the modification.

I don't understand what you mean. I would love to see code example.