Merge pull request #17 from viniciusfdasilva/dev

Option result and error
This commit is contained in:
Vinicius Silva 2024-01-27 22:45:17 -03:00 committed by GitHub
commit 7ea0035d2c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 47 additions and 0 deletions

View File

@ -0,0 +1,47 @@
struct User{
id int
name string
}
struct Repo{
users []User
}
fn (r Repo) find_user_by_id(id int) !User{
for user in r.users {
if user.id == id {
return user
}
}
return error('User ${id} not found')
}
fn (r Repo) find_user_by_id2(id int) ?User{
for user in r.users {
if user.id == id {
return user
}
}
return none
}
fn main(){
repo := Repo{
users: [User{1, 'Andrew'},
User{2, 'Bob'},
User{10, 'Charles'}
]
}
user := repo.find_user_by_id(10) or {
println(err)
return
}
println(user.id)
println(user.name)
user2 := repo.find_user_by_id2(10) or { return }
println(user2)
}