-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
您好,我最近正在研讀您所撰寫的此書,想向您請教有關範例程式碼的問題:
以下為 ch05_error_handling 章節中 05-07 範例 impl SecurePassword 的程式碼區塊:
impl SecurePassword {
pub fn new(password: &str) -> Result<Self, PasswordError> {
if password.len() < 8 {
Err(PasswordError::TooShort(password.len()))
} else if !password.chars().any(char::is_numeric) {
Err(PasswordError::MissingNumber)
} else {
Ok(Self {
value: password.to_string(),
})
}
}
}因為此程式的目的為檢測密碼的「總字元數」而非「總位元組數」,是不是應該使用 password.chars().count() 而非 password.len()呢:
impl SecurePassword {
pub fn new(password: &str) -> Result<Self, PasswordError> {
if password.chars().count() < 8 {
Err(PasswordError::TooShort(password.chars().count()))
} else if !password.chars().any(char::is_numeric) {
Err(PasswordError::MissingNumber)
} else {
Ok(Self {
value: password.to_string(),
})
}
}
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels