Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use correctly the crate text_io?

I have a small problem with a crate:
This code works:

fn main() {
    println!("Celsius to farenheit");
    print!("Insert Celsius: ");
    let celsius: f32 = read!();
    println!("Celsius to Farenheit is: {}", cel_to_far(celsius));
}

But if I use what is in the manual of text_io:

fn main() {
    println!("Celsius to farenheit");
    let celsius: f32 = read!("Insert Celsius: {}!");
    println!("Celsius to Farenheit is: {}", cel_to_far(celsius));
}

It panics:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Celsius to farenheit
6
thread 'main' panicked at src/main.rs:4:24:
called `Result::unwrap()` on an `Err` value: UnexpectedValue(73, Some(54))
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

This doesn’t and I think I’m following the correct instructions:

>Solution :

Both snippets are not equivalent!

The first one prints Insert Celsius: then reads an integer, the second one will read Insert Celsius: then an integer and then an ! meaning you have to type all that, it doesn’t print it. That is the second one expects you to type all of Insert Celsius: 123!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading