A minimal library to provide similar name suggestions like "Did you mean?" This library provides suggestion traits for all collection types in the standard library.
This library is intended to suggest a candidate from a list of unknown suggestions until runtime, in addition to the suggestion feature already available in clap
.
Add the following to your Cargo.toml
:
[dependencies]
suggest = "0.5"
This example can be executed by the cargo run --example simple
command.
use suggest::Suggest;
fn main() {
let input = "instakk";
let list_commands = vec!["update", "install"];
if list_commands.contains(&input) {
return;
}
if let Some(sugg) = list_commands.suggest(input) {
println!("No command named `{}` found.", input);
println!("Did you mean `{}`?", sugg);
}
}
$ cargo run
No command named `instakk` found.
Did you mean `install`?
use suggest::Suggest;
fn main() {
let input = "paoc";
let list_commands = vec!["poac", "poacpp"];
if list_commands.contains(&input) {
return;
}
if let Some(sugg) = list_commands.suggest_by(input, 2) {
println!("No command named `{}` found.", input);
println!("Did you mean `{}`?", sugg);
}
}
$ cargo run
No command named `paoc` found.
Did you mean `poac`?
Please let me know if anything is left out through issues or pull requests.
LinkedList
VecDeque
Vec
HashMap
BTreeMap
To suggest keys, use suggest::SuggestKey
trait.
BTreeSet
HashSet
BinaryHeap
[T; N]
: primitive array[T]
: slices
Contributions, including issues and pull requests, are very welcome.
$ cargo build
$ cargo build
$ cargo test
$ git tag v0.1.0
$ git push origin v0.1.0
$ cargo publish