Rust is a new systems programming language made by Mozilla , it’s extremely fast !
In this tutorial we’ll develop a program in Rust that translate words using the Yandex Translator API.
Installing Rust
Rust is always changing , this tutorial is made with version 1.14.0 (released in December 22, 2016).
To install the latest Rust version , run this command in your Terminal
curl https://sh.rustup.rs -sSf | sh
Create a new project
Cargo is a package manager for Rust , it also create projects , build , and run it .
To create a new Rust project , run
cargo new RustTranslator --bin
– -bin indicates that we are creating a binary program and not a library.
Once you created the project , two files will be created
- Cargo.toml : contains the project dependencies and information (developer,version …)
- main.rs : contains our source code
Go to src , with
cd src
to run the program , run
cargo run
This will prompt a simple hello world
Hello, world!
Import dependencies
Open Cargo.toml and add
[dependencies] hyper = "0.10.0" rustc-serialize = "0.3" hyper-native-tls = "0.2"
Our project needs those libraries :
- Hyper : will help us make HTTP requests to get the word’s translation from Yandex API
- rustc-serialize : will help us parse the HTTP result
- hyper-native-tls : Since Yandex Translator API is available in HTTPS , we need this library to make HTTP request to encrypted web services
To import it to the project , run
cargo update
Just wait , this depends on your Internet speed.
Coding …
We just finished importing the libraries we need , now we have to tell Rust that we will use it in our program . To do so , open main.rs and add :
extern crate hyper; extern crate rustc_serialize; extern crate hyper_native_tls; use hyper::client::Client; use std::io::Read; use hyper::net::HttpsConnector; use hyper_native_tls::NativeTlsClient; use std::io;
this will compile and link the libraries to our project.
To get the word that the user want to translate , we need to read user input from the keyboard , so add this to our “main” function
let mut word = String::new(); io::stdin().read_line(&mut word) .expect("failed to read the word");
The next step is to get the language that the user want to translate the word to. We will do the same thing :
let mut language = String::new(); io::stdin().read_line(&mut language) .expect("failed to read the language");
Every request to Yandex Translator API , is made to a link as below
https://translate.yandex.net/api/v1.5/tr.json/translate?key=YOUR-KEY&text=THE-WORD&lang=THE-LANGUAGE&format=json
So , you have to put your own key ,you can get a new one from here (If you just want to test the program , you can use mine , I’ll put it below . Please don’t make too much requests !). In every request we make , we have to change the word we want to translate and the language that the user chose . To do so , we’ll create a new variable called “link” and concatenate “language” and “word” to it .
let mut link = "https://translate.yandex.net/api/v1.5/tr.json/translate?key=trnsl.1.1.20170114T114459Z.7de5877240d656e7.e5bdc730b5945491203a9b7a0ee0cfd7af579369&text=".to_string(); link = link + &word; link.push_str("&lang="); link= link+&language; link.push_str("&format=json");
Once we have the word , the language and the link , We are ready to make the HTTP request to get the result from Yandex Translator
let ssl = NativeTlsClient::new().unwrap(); let connector = HttpsConnector::new(ssl); let client = Client::with_connector(connector); // the string that will contain the request result let mut translationResult = String::new(); // send the request client.get(&link) .send() .unwrap() // put the result to the string declared above .read_to_string(&mut translationResult) .unwrap();
To be able to extract the translated word , we’ll format the HTTP result to JSON
let data = rustc_serialize::json::Json::from_str(&translationResult).unwrap();
Then , get the JSON object.
let obj = data.as_object().unwrap();
This JSON object contains the translated word and the source-destination language. To extract this information from the JSON object , add :
let translatedWord = obj.get("text").unwrap(); let translatedFromTo = obj.get("lang").unwrap();
Finally , print the result
println!("translation is : {} ",translatedWord[0]); println!("translated from-to : {} ",translatedFromTo);
“Rust Translator” is ready to test
To test our project , run
cargo run
Add some instructions to the user ( println!(“do that”) ), The result will be :
---------------------------------------------------------------------------- --- Welcome to Rust Translator by Marwen Doukh - Mozilla Tunisia --- ---------------------------------------------------------------------------- Please enter the word you want to translate Hello Mozilla Please choose the language that you want to translate to (eg : choose en if you want English) English : en Arabic : ar French : fr German : de ---------------------- Translate to : de translation is : "Hallo Mozilla" translated from-to : "fi-de"
This is it ! As easy as that !
Many projects nowadays are made using Rust like Servo and the Rust compiler itself. Mozilla is working hard everyday to improve this programming language , you can help it by contributing , You’ll shape the next version of Rust !
To get the full source code of “Rust Translator”, you can find it on Github . Feel free to change it !