Guest User

Untitled

a guest
Jul 16th, 2018
526
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. #[derive(Debug)]
  2. struct User {
  3. username: String,
  4. email: String,
  5. sign_in_count: u64,
  6. active: bool,
  7. }
  8.  
  9. impl User { // in this block, you can add methods to the struct
  10. fn new(username: String) -> User { // static method, works like a constructor
  11. User {
  12. email: format!("{}@user.com", &username), // use the & operator to only borrow the string
  13. username: username, // after that, give (move) the name to it's user
  14. sign_in_count: 1,
  15. active: true,
  16. }
  17. }
  18. }
  19.  
  20. fn main() {
  21. // there are methods to avoid this .to_string(), but it's ok for the beginning.
  22. let u1 = User::new("Chuck Norris".to_string());
  23. println!("Hello {:?}", u1)
  24. }
Add Comment
Please, Sign In to add comment