pub struct Password<'a> { /* private fields */ }Available on crate feature
password only.Expand description
Renders a password input prompt.
§Example
use dialoguer::Password;
fn main() {
let password = Password::new()
.with_prompt("New Password")
.with_confirmation("Confirm password", "Passwords mismatching")
.interact()
.unwrap();
println!("Your password length is: {}", password.len());
}Implementations§
Source§impl Password<'_>
impl Password<'_>
Sourcepub fn with_prompt<S: Into<String>>(self, prompt: S) -> Self
pub fn with_prompt<S: Into<String>>(self, prompt: S) -> Self
Sets the password input prompt.
Examples found in repository?
examples/password.rs (line 5)
3fn main() {
4 let password = Password::with_theme(&ColorfulTheme::default())
5 .with_prompt("Password")
6 .with_confirmation("Repeat password", "Error: the passwords don't match.")
7 .validate_with(|input: &String| -> Result<(), &str> {
8 if input.chars().count() > 3 {
9 Ok(())
10 } else {
11 Err("Password must be longer than 3")
12 }
13 })
14 .interact()
15 .unwrap();
16
17 println!(
18 "Your password is {} characters long",
19 password.chars().count()
20 );
21}Sourcepub fn report(self, val: bool) -> Self
pub fn report(self, val: bool) -> Self
Indicates whether to report confirmation after interaction.
The default is to report.
Sourcepub fn with_confirmation<A, B>(self, prompt: A, mismatch_err: B) -> Self
pub fn with_confirmation<A, B>(self, prompt: A, mismatch_err: B) -> Self
Enables confirmation prompting.
Examples found in repository?
examples/password.rs (line 6)
3fn main() {
4 let password = Password::with_theme(&ColorfulTheme::default())
5 .with_prompt("Password")
6 .with_confirmation("Repeat password", "Error: the passwords don't match.")
7 .validate_with(|input: &String| -> Result<(), &str> {
8 if input.chars().count() > 3 {
9 Ok(())
10 } else {
11 Err("Password must be longer than 3")
12 }
13 })
14 .interact()
15 .unwrap();
16
17 println!(
18 "Your password is {} characters long",
19 password.chars().count()
20 );
21}Sourcepub fn allow_empty_password(self, allow_empty_password: bool) -> Self
pub fn allow_empty_password(self, allow_empty_password: bool) -> Self
Allows/Disables empty password.
By default this setting is set to false (i.e. password is not empty).
Sourcepub fn interact(self) -> Result<String>
pub fn interact(self) -> Result<String>
Enables user interaction and returns the result.
If the user confirms the result is Ok(), Err() otherwise.
The dialog is rendered on stderr.
Examples found in repository?
examples/password.rs (line 14)
3fn main() {
4 let password = Password::with_theme(&ColorfulTheme::default())
5 .with_prompt("Password")
6 .with_confirmation("Repeat password", "Error: the passwords don't match.")
7 .validate_with(|input: &String| -> Result<(), &str> {
8 if input.chars().count() > 3 {
9 Ok(())
10 } else {
11 Err("Password must be longer than 3")
12 }
13 })
14 .interact()
15 .unwrap();
16
17 println!(
18 "Your password is {} characters long",
19 password.chars().count()
20 );
21}Source§impl<'a> Password<'a>
impl<'a> Password<'a>
Sourcepub fn validate_with<V>(self, validator: V) -> Self
pub fn validate_with<V>(self, validator: V) -> Self
Registers a validator.
§Example
use dialoguer::Password;
fn main() {
let password: String = Password::new()
.with_prompt("Enter password")
.validate_with(|input: &String| -> Result<(), &str> {
if input.chars().count() > 8 {
Ok(())
} else {
Err("Password must be longer than 8")
}
})
.interact()
.unwrap();
}Examples found in repository?
examples/password.rs (lines 7-13)
3fn main() {
4 let password = Password::with_theme(&ColorfulTheme::default())
5 .with_prompt("Password")
6 .with_confirmation("Repeat password", "Error: the passwords don't match.")
7 .validate_with(|input: &String| -> Result<(), &str> {
8 if input.chars().count() > 3 {
9 Ok(())
10 } else {
11 Err("Password must be longer than 3")
12 }
13 })
14 .interact()
15 .unwrap();
16
17 println!(
18 "Your password is {} characters long",
19 password.chars().count()
20 );
21}Sourcepub fn with_theme(theme: &'a dyn Theme) -> Self
pub fn with_theme(theme: &'a dyn Theme) -> Self
Creates a password input prompt with a specific theme.
§Example
use dialoguer::{theme::ColorfulTheme, Password};
fn main() {
let password = Password::with_theme(&ColorfulTheme::default())
.interact()
.unwrap();
}Examples found in repository?
examples/password.rs (line 4)
3fn main() {
4 let password = Password::with_theme(&ColorfulTheme::default())
5 .with_prompt("Password")
6 .with_confirmation("Repeat password", "Error: the passwords don't match.")
7 .validate_with(|input: &String| -> Result<(), &str> {
8 if input.chars().count() > 3 {
9 Ok(())
10 } else {
11 Err("Password must be longer than 3")
12 }
13 })
14 .interact()
15 .unwrap();
16
17 println!(
18 "Your password is {} characters long",
19 password.chars().count()
20 );
21}Trait Implementations§
Auto Trait Implementations§
impl<'a> Freeze for Password<'a>
impl<'a> !RefUnwindSafe for Password<'a>
impl<'a> !Send for Password<'a>
impl<'a> !Sync for Password<'a>
impl<'a> Unpin for Password<'a>
impl<'a> !UnwindSafe for Password<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more