Console Input/Output

C++:

#include <iostream>

using namespace std;

int main() 
{
    string name;
    cout << "Enter your name : ";
    getline(cin, name);
    cout << "Hello " << name << "!" << endl;
    return 0;
}

Rust :

use std::io::{self, BufRead, Write};

fn main() 
{
    let stdin = io::stdin();
    print!("Enter your name : "); 
    io::stdout().flush();
    let mut name = String::new();
    stdin.lock().read_line(&mut name);
    println!("Hello {} !", name.trim());
}

When you compile this Rust program, you will get several warning messages such as : "warning: unused `std::result::Result` which must be used". At the moment, you can ignore these messages, just make the program work first. The purpose of this book is to help you move from C++ to Rust as quickly as possible, so error handling is ignored here, which causes the warning messages. When you get familiar with some Rust codes, you can come back to the official tutorials on Rust website to see how they handle the I/O errors with detail explanation.

results matching ""

    No results matching ""