Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[derive(Debug)]
- struct User {
- username: String,
- email: String,
- sign_in_count: u64,
- active: bool,
- }
- impl User { // in this block, you can add methods to the struct
- fn new(username: String) -> User { // static method, works like a constructor
- User {
- email: format!("{}@user.com", &username), // use the & operator to only borrow the string
- username: username, // after that, give (move) the name to it's user
- sign_in_count: 1,
- active: true,
- }
- }
- }
- fn main() {
- // there are methods to avoid this .to_string(), but it's ok for the beginning.
- let u1 = User::new("Chuck Norris".to_string());
- println!("Hello {:?}", u1)
- }
Add Comment
Please, Sign In to add comment