Crates.io | t9 |
lib.rs | t9 |
version | 0.2.2 |
source | src |
created_at | 2019-11-11 17:41:35.755764 |
updated_at | 2019-11-11 23:35:57.239102 |
description | Easily create port number from words and vice versa |
homepage | |
repository | https://github.com/fifth-postulate/T9 |
max_upload_size | |
id | 180322 |
size | 62,139 |
Predictive text in Rust
I learned a neat trick from ag_dubs to come up with ports for your application. They used it during various RustBridge events.
The trick is to take a word that is relevant for your application and looking at the keypad of a phone.
By Sakurambo - Created using Adobe Illustrator CS2, Public Domain, https://commons.wikimedia.org/w/index.php?curid=2048341
Lookup the corresponding number for each letter and that will be your port number.
Let's take the word rust
. Looking at the a keypad we find the following numbers
Letter | Number |
---|---|
r | 7 |
u | 8 |
s | 7 |
t | 8 |
To the port number for your application would be 7878
.
Figuring out the correct keypad is hard work. Luckily that weher the t9
crate comes in. The following code could be used to figure the corresponding port for rust as well.
extern crate t9;
use std::env;
use t9::pad;
fn main() {
let args: Vec<String> = env::args().collect();
let port_name = &args[1];
let port = pad::digits_for(port_name);
println!("{}", port)
}
Calling it with cargo run --examples port -- rust
returns 7878
as expected. port.rs
can be found in the examples directory.
t9
also allows you to do a reverse lookup. I.e., what word was used for a given port number?
extern crate t9;
use std::env;
use std::fs::File;
use std::io::{self, BufRead, BufReader};
use t9::{pad, tree::Tree};
fn main() -> io::Result<()> {
let mut tree = Tree::empty();
let file = File::open("/usr/share/dict/american-english")?;
let reader = BufReader::new(file);
for word in reader.lines() {
tree.add(word?);
}
let args: Vec<String> = env::args().collect();
let port = &args[1];
let digits = pad::digits_for(port);
let words = tree.words_at(digits);
for word in words {
println!("{}", word)
}
Ok(())
}
Run with cargo run --release --example t9 -- 7878
returns the rust
as expected. An more elaborate example, allowing you to specify the dictionary file, can be found in the examples directory.