Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[derive(Debug)]
- struct YourType {
- value: usize
- }
- impl YourType {
- // This takes an "immutable" (non-changable) reference to the object its called on
- fn do_something(&self) -> usize {
- self.value + 1
- }
- // This takes an "mutable" (changable) reference to the object its called on
- fn do_edit(&mut self) {
- self.value += 1
- }
- // Consumes the object its called on
- fn eat(self) -> usize {
- self.value + 2
- }
- }
- fn main() {
- let mut a = YourType { value: 1 };
- println!("a: {:?}", a);
- a.do_edit();
- println!("b: {:?}", a);
- println!("consumed: {}", a.eat());
- // if you would try to use a beyond this point it would throw an error,
- // since a has been consumed
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement