Iterator
Runtime errors like connection-refused or file-not-found are handled with the Result
type, but matching this type on every call can be cumbersome. The try-operator ?
is used to return errors to the caller. It lets you turn the common
match some_expression {
Ok(value) => value,
Err(err) => return Err(err),
}
이렇게 짧게 쓸 수 있습니다
some_expression?
이제 우리 예제에 적용해 보겠습니다:
Speaker Notes
This slide should take about 5 minutes.
?
를 사용하도록 read_username
함수를 단순화합니다.
키 포인트:
username
변수는Ok(string)
이거나Err(error)
일 수 있습니다.fs::write
메서드를 사용하여 파일이 없거나, 비었거나, 중복되는 경우 등을 테스트해 봅니다.- Note that
main
can return aResult<(), E>
as long as it implementsstd::process::Termination
. In practice, this means thatE
implementsDebug
. The executable will print theErr
variant and return a nonzero exit status on error.