Skip to content
Kavachकवच

User Input: Ask for a password

● Beginner3 min

term.prompt_secret reads a password without showing it, and keeps it secret afterwards too.

use std.term

fn main() {
    const pin = term.prompt_secret("Card PIN?") ?? return
    print(pin)
}

The program ran and printed:

kavach run password.kav

Card PIN? Secret(text: <secret>)
password.kav: Kavach code, then what running it prints

Typed by the user: 4321. What is typed isn't part of the program's output, so it doesn't show in the box.

The typing isn't shown on the screen. The answer is a Secret, and printing it shows <secret>, never the PIN: a password can't end up in a log by accident.

?? return ends the program quietly if no PIN was typed.