-6

I'm making a program in rust has an output like this but I'm failed.

Enter String: Hi

How many copies of String you need: 3

3 Copies of Hi are HiHiHi

but my code gives me this

HI

HI

HI

while i < index {

    println!("{}", text);
    i += 1;

}
Arsalan Ahmed
  • 188
  • 1
  • 12
  • Are you doing this in `java`? – João Bravo Jun 10 '19 at 21:58
  • 2
    Please take the time to look at the formatting of your post. It's very difficult to tell which parts are output from the compiler or the program, which parts are input by you, and which parts are text from your question. – Shepmaster Jun 10 '19 at 23:04
  • 1
    Please take the time to look at the formatting of your post. It's very difficult to tell which parts are output from the compiler or the program, which parts are input by you, and which parts are text from your question. – Shepmaster Jun 10 '19 at 23:04
  • 1
    It's hard to answer your question because it doesn't include a [MCVE]. We can't tell what crates (and their versions), types, traits, fields, etc. are present in the code. It would make it easier for us to help you if you try to reproduce your error on the [Rust Playground](https://play.rust-lang.org) if possible, otherwise in a brand new Cargo project, then [edit] your question to include the additional info. There are [Rust-specific MCVE tips](//stackoverflow.com/tags/rust/info) you can use to reduce your original code for posting here. Thanks! – Shepmaster Jun 10 '19 at 23:04
  • It *appears* that you are stating that the output of the program is `HI` when the input was `Hi`. It's **very improbable** that the compiler magically changed the case of your string (see also [Why is capitalizing the first letter of a string so convoluted in Rust?](https://stackoverflow.com/q/38406793/155423)), which seems to indicate that you didn't exactly copy the input and output. If you don't copy the exact code, input, and output, then you are asking us to diagnose a problem based on false premises. – Shepmaster Jun 10 '19 at 23:06
  • @JoãoBravo your edit was incorrect (and the reviewers should not have approved it). You added characters (the backticks) and changed the output provided by the OP (capital `I` to lowercase `i`). This fundamentally changes the question. I've rolled back the changes. – Shepmaster Jun 11 '19 at 12:31
  • I don't think it does. The question is primarily about output with/without newlines. I assumed the capital I and the lowercase i have nothing to do with it – João Bravo Jun 11 '19 at 18:34

1 Answers1

2

println adds a newline by default (that is what the ln stands for, line).

Try to use the print macro instead.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
João Bravo
  • 206
  • 4
  • 16