Advertisement
cwchen

[Rust] public and private methods

Aug 28th, 2017
3,634
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.42 KB | None | 0 0
  1. // We use mod to create a non-public block
  2. mod lib {
  3.     pub struct Car;
  4.  
  5.     impl Car {
  6.         // Public method
  7.         pub fn run(& self) {
  8.             // Call private method
  9.             self.drive();
  10.         }
  11.     }
  12.  
  13.     impl Car {
  14.         // Private method
  15.         fn drive(& self) {
  16.             println!("Driving a car...");
  17.         }
  18.     }
  19. }
  20.  
  21. fn main() {
  22.     let car = lib::Car{};
  23.     car.run();
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement