0

I wrote a little program in Rust that just doesn't work.

use std::io;

fn main() {
    println!("Enter a string: ");
    let mut input = String::new();
    io::stdin().read_line(&mut input).unwrap();

    println!("you have entered:\n{}", input);

    println!("enter a char which you wish to replace: ");
    let mut char_to_be_replaced = String::new();
    io::stdin().read_line(&mut char_to_be_replaced).unwrap();
    println!("you have entered: {}", char_to_be_replaced);

    println!("enter a replacement char:");
    let mut replacement_char = String::new();
    io::stdin().read_line(&mut replacement_char).unwrap();
    println!("replacement char is: {}", replacement_char);

    let result = input.replace(&char_to_be_replaced, &replacement_char);
    println!("end result is: ");
    println!("{}", result);
}

input is "asd, asd"

char_to_be_replaced is ","

replacement_char is "."

to my horror result is "asd, asd" instead of "asd. asd"

beepbeep
  • 184
  • 1
  • 2
  • 11

1 Answers1

1

Your strings end with a newline. You can usetrim to remove them.

Julian
  • 2,724
  • 1
  • 15
  • 21