Atividade percorrida rust

use std::io;

fn main() {
    // Entrada da distancia
    println!("Digite a distancia a ser percorrida (em km):");
    let mut distancia = String::new();
    io::stdin().read_line(&mut distancia).expect("Erro ao ler a distancia");
    let distancia: f64 = distancia.trim().parse().expect("Por favor, digite um numero valido");

    // Entrada da velocidade media
    println!("Digite a velocidade media do veiculo (em km/h):");
    let mut velocidade = String::new();
    io::stdin().read_line(&mut velocidade).expect("Erro ao ler a velocidade");
    let velocidade: f64 = velocidade.trim().parse().expect("Por favor, digite um numero valido");

    // Calculo do tempo
    let tempo = distancia / velocidade;

    // Resultado
    println!("\nTempo estimado de viagem: {:.2} horas", tempo);
}

(Playground)

Output:

Digite a distancia a ser percorrida (em km):

Errors:

   Compiling playground v0.0.1 (/playground)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.71s
     Running `target/debug/playground`

For the case that something does not work as you expected, it might be related to the fact that in Rust Playground interactive input like readline() does not work, so you might get only empty strings and perhaps resulting from that a division by zero error.

1 Like

It works. You just have to enter the data:

With 12+Enter+12+Enter I get:

Digite a distancia a ser percorrida (em km):
Digite a velocidade media do veiculo (em km/h):

Tempo estimado de viagem: 1.00 horas

You can submit data to STDIN via the white input box below the output box (left of the button labeled SEND).

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.