Rust -004 — Struct
Structs are similar to tuples:
struct User { active: bool, username: String, email: String, sign_in_count: u64,
}
Note that the entire instance must be mutable; Rust doesn’t allow us to mark only certain fields as mutable.
we can use the field init shorthand syntax to rewrite build_user
so that it behaves exactly the same
struct User {
active: bool,
username: String,
email: String,
sign_in_count: u64,
}
fn build_user(email: String, username: String) -> User {
User {
email,
username,
active: true,
sign_in_count: 1,
}
}fn main() {
let user1 = build_user(
String::from(“someone@example.com”),
String::from(“someusername123”),
);
}
Creating Instances From Other Instances With Struct Update Syntax
struct User {
active: bool,
username: String,
email: String,
sign_in_count: u64,
}fn main() {
let user1 = User {
email: String::from(“someone@example.com”),
username: String::from(“someusername123”),
active: true,
sign_in_count: 1,
};let user2 = User {
email: String::from(“another@example.com”),
..user1
};
}
The ..user1
must come last to specify that any remaining fields should get their values from the corresponding fields in user1
, but we can choose to specify values for as many fields as we want in any order, regardless of the order of the fields in the struct’s definition.
Tuple Structs
Rust also supports structs that look similar to tuples, called tuple structs.
Lifetimes
It’s also possible for structs to store references to data owned by something else, but to do so requires the use of lifetimes. Lifetimes ensure that the data referenced by a struct is valid for as long as the struct is
Debug
Rust does include functionality to print out debugging information, but we have to explicitly opt in to make that functionality available for our struct. To do that, we add the outer attribute #[derive(Debug)]
just before the struct definition.
Methods
Methods are similar to functions: we declare them with the fn
keyword and a name, they can have parameters and a return value, and they contain some code that’s run when the method is called from somewhere else and their first parameter is always self
The &self
is actually short for self: &Self
. Within an impl
block, the type Self
is an alias for the type that the impl
block is for.
Associated Functions
All functions defined within an impl
block are called associated functions because they’re associated with the type named after the impl.
We can define associated functions that don’t have self
as their first parameter (and thus are not methods) because they don’t need an instance of the type to work with.
Associated functions that aren’t methods are often used for constructors that will return a new instance of the struct. For example, we could provide an associated function that would have one dimension parameter and use that as both width and height, thus making it easier to create a square Rectangle
rather than having to specify the same value twice.
Reference:
https://doc.rust-lang.org/book/ch05-00-structs.html
