Structure
Same as classes but only with data attributes
Struct
Structure or struct is like a class but only with attributes. One useful feature that I found with rust is short-handing. For instance take an example below
struct User {
email: String,
username: String,
password: String,
}
impl User {
fn new(email: String, password: String, username: String) -> Self {
Self {
password,
email,
username,
}
}
}
As you could see above in the new method, you don't really have to pass the parameters in the order they are described in the struct user. Just the passing the arguments with same name would be more than sufficient for instantiating the User struct.
fn new(email: String, password: String, username: String) -> Self {
Self {
password,
email,
username,
}
}
In the example above, the passed arguments (password, email, username) have same name as struct but they are not the order as they have been defined in the struct User
Last updated
Was this helpful?