/blog/archive/My first crate simple_prompts on crates-io

My first crate simple_prompts on crates.io

created: 2020-12-26T15:47:01Z
modified: 2021-01-31T19:35:44Z

So learning the ins and outs of whole ecosystem is always neat. A well thought out and complete ecosystem is particularly interesting and that’s what I’m discovering Rust is. This crate is super simple, it’s just a wrapper around some very basic usage of Rustyline. I found myself getting ready to copy some functions from the CLI CMS I’m building for this blog into a couple of practice projects and thought, hey I don’t need to do that and I can learn a bit more about Rust crates in the process, so I made simple_prompts instead of cutting and pasting code.

The crate like the code is extremely simple, just import it and it exposes 2 public functions for generating prompts: one, for showing a custom prompt that accepts input in the CLI; the other, for doing the same but prepopulating a value in the CLI input line.


[dependencies ]
simple_prompts = "0.1.2"


use simple_prompts::{prompt, edit_prompt};

fn main() {
    let mut our_input: String = prompt("Enter some text: ");
    our_input = edit_prompt("You entered this, change it: ", &our_input);
    println!("Final value is: {}", our_input)

Thats it, it doesn’t do anything else.

I also used it as a chance to get into document comments and doc-tests. Something I need to get better at, but have avoided to date. So that was interesting.

/// Creates a CLI prompt from a &str for input and returns a String.
/// Example
/// ```
/// use simple_prompts::{prompt};
/// pub fn do_this() {
///   let value = prompt("Enter a value: ");
///   println!("{}", value)
/// }
/// ```

I just stuck that above the functions in lib.rs and Cargo handles everything else for you. And by everything else I mean documentation is done, and there are now embedded tests that can be automatically run.

  • Creative Commons License
  • Author: Gatewaynode