Sunday, December 23, 2018

Rust. Install and run the rust project.

Install Rust (in Ubuntu18)

$ sudo apt install rustc
$ sudo apt install cargo
Check if you successfully installed it:
$ rustc --version
rustc 1.30.0

Create a project of Rust

We will use cargo to create a rust project.
$ cargo new your_project_name --bin
Or if you want to make a library:
$ cargo new your_project_name --lib

Run a project

You can compile the project by:
$ cd ./your_project_name
$ cargo build
Now you can run it:
$ ./target/debug/your_project_name
Also you can comiple and run the project by:
$ cd ./your_project_name
$ cargo run

Receive input and print it

This code is from here. The code is as follows:
fn main() {
    println!("{}", get_input());
}

fn get_input() -> String {
    use std::io::{stdin,stdout,Write};
    let mut s=String::new();
    print!("Please enter some text: ");
    let _ = stdout().flush();
    stdin().read_line(&mut s).expect("Did not enter a correct string");
    if let Some('\n')=s.chars().next_back() {
        s.pop();
    }
    if let Some('\r')=s.chars().next_back() {
        s.pop();
    }
    return s;
}
Replace the inside of main.rs with the code above. Compile and run it.
$ cargo run
  Finished dev [unoptimized + debuginfo] target(s) in 0.01s                   
  Running `target/debug/excel_to_pdf`
Please enter some text: test
test