summaryrefslogtreecommitdiff
path: root/structure/src/main.rs
diff options
context:
space:
mode:
authorJordan Gong <jordan.gong@protonmail.com>2020-08-13 16:47:02 +0800
committerJordan Gong <jordan.gong@protonmail.com>2020-08-13 16:47:02 +0800
commit95aaa2790a75e9e5476a53ff52d324b4201ef39b (patch)
treedb7b12291a0aa17e3b444785555a3350a9b925a7 /structure/src/main.rs
parent0cebf66a1816e5bf3c005dff6fb17bba55c1517f (diff)
Introduce structs
Diffstat (limited to 'structure/src/main.rs')
-rw-r--r--structure/src/main.rs43
1 files changed, 43 insertions, 0 deletions
diff --git a/structure/src/main.rs b/structure/src/main.rs
new file mode 100644
index 0000000..81df2d9
--- /dev/null
+++ b/structure/src/main.rs
@@ -0,0 +1,43 @@
+/* define */
+struct User {
+ username: String,
+ email: String,
+ sign_in_count: u64,
+ active: bool,
+}
+
+/* tuple structs */
+struct Color(i32, i32, i32);
+struct Point(i32, i32, i32);
+
+fn main() {
+ /* create an instance */
+ let mut user1 = User {
+ email: String::from("someone@example.com"),
+ username: String::from("someusername123"),
+ active: true,
+ sign_in_count: 1,
+ };
+
+ user1.email = String::from("anotheremal@example.com");
+
+ /* update syntax */
+ let user2 = User {
+ email: String::from("another@email.com"),
+ username: String::from("anotherusername567"),
+ ..user1
+ };
+
+ let black = Color(0, 0, 0);
+ let origin = Point(0, 0, 0);
+}
+
+fn build_user(email: String, username: String) -> User {
+ User {
+ email, /* field init shorthad */
+ username,
+ active: true,
+ sign_in_count: 1,
+ }
+}
+