Created
June 2, 2020 12:16
-
-
Save beardedtim/98a0ccaef126cb33fece990e93a62299 to your computer and use it in GitHub Desktop.
Autocomplete in Rust
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io; | |
#[derive(Debug)] | |
struct AutoCompleteRequest { | |
value: String | |
} | |
#[derive(Debug)] | |
struct AutoCompleteResponse { | |
suggestions: Vec<String> | |
} | |
fn get_autocomplete(request: AutoCompleteRequest, services: Vec<String>) -> AutoCompleteResponse { | |
let mut vec = Vec::new(); | |
let request_len = request.value.len(); | |
for service in &services { | |
println!("{}", service); | |
println!("{}", request.value); | |
if service.to_lowercase()[0..request_len -1] == request.value.to_lowercase().trim().to_string() { | |
vec.push(service.clone().to_string()); | |
} | |
} | |
AutoCompleteResponse { | |
suggestions: vec | |
} | |
} | |
fn main() { | |
println!("Autocomplete For Rust!"); | |
println!("\nPlease input your starting string\n"); | |
let mut input = String::new(); | |
io::stdin() | |
.read_line(&mut input) | |
.expect("Failed to read line"); | |
println!("\nI am looking for services that match {}", input); | |
let response = get_autocomplete(AutoCompleteRequest{ value: input }, vec!["Slack".to_string(), "Salesforce".to_string()]); | |
println!("\nResponse is {:#?}", response); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Autocomplete For Rust! | |
Please input your starting string | |
sl | |
I am looking for services that match sl | |
Slack | |
sl | |
Salesforce | |
sl | |
Response is AutoCompleteResponse { | |
suggestions: [ | |
"Slack", | |
], | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment