Skip to content

Commit af5f522

Browse files
authored
Count characters in password example instead of bytes (console-rs#276)
* Count characters in `password` example * Fix password example too
1 parent 9baa851 commit af5f522

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

examples/password.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ fn main() {
55
.with_prompt("Password")
66
.with_confirmation("Repeat password", "Error: the passwords don't match.")
77
.validate_with(|input: &String| -> Result<(), &str> {
8-
if input.len() > 3 {
8+
if input.chars().count() > 3 {
99
Ok(())
1010
} else {
1111
Err("Password must be longer than 3")
@@ -14,5 +14,8 @@ fn main() {
1414
.interact()
1515
.unwrap();
1616

17-
println!("Your password is {} characters long", password.len());
17+
println!(
18+
"Your password is {} characters long",
19+
password.chars().count()
20+
);
1821
}

src/prompts/password.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl Password<'_> {
8686

8787
/// Enables user interaction and returns the result.
8888
///
89-
/// If the user confirms the result is `true`, `false` otherwise.
89+
/// If the user confirms the result is `Ok()`, `Err()` otherwise.
9090
/// The dialog is rendered on stderr.
9191
pub fn interact(self) -> Result<String> {
9292
self.interact_on(&Term::stderr())
@@ -159,7 +159,7 @@ impl<'a> Password<'a> {
159159
/// let password: String = Password::new()
160160
/// .with_prompt("Enter password")
161161
/// .validate_with(|input: &String| -> Result<(), &str> {
162-
/// if input.len() > 8 {
162+
/// if input.chars().count() > 8 {
163163
/// Ok(())
164164
/// } else {
165165
/// Err("Password must be longer than 8")

0 commit comments

Comments
 (0)