Skip to content

Commit

Permalink
tour: add lesson for newtype (#384)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoorkin authored Dec 25, 2024
1 parent 03bc821 commit 4530fe3
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ type UserName String
fn main {
let user_id : UserId = UserId(1)
let user_name : UserName = UserName("Alice")
let UserId(id) = user_id
let UserName(name) = user_name
println(user_id._)
println(user_name._)
// use some pattern matching to extract the values
let UserId(id) = user_id
let UserName(name) = user_name
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Newtype

Newtypes are similar to enums with only one constructor (with the same name as the newtype itself). You can use the constructor to create values of the newtype and use `._` to extract the internal representation.

*Pattern matching* can also be used.

0 comments on commit 4530fe3

Please sign in to comment.