-1

I am learning rust and I always try some basic codes while learning any language. When I learned the syntax for loops in Rust, I tried to write a code for a half pyramid pattern like I always do. But it dosen't seem to be working. Cam someone please teach me how to?

This is the code that I wrote:

#![allow(unused_variables)]
#![allow(dead_code)]
#![allow(unused_mut)]

fn main()
{
    let mut x: isize;
    let mut y: isize; 

    y = 5; 

    for x in 0..=y
    {
        for _ in 0..=x
        {
            println!("*");
        }
        println!();
    }
}

I am expecting an output:

*
**
***
****
*****

But what I am getting is:

*

*
*

*
*
*

*
*
*
*

*
*
*
*
*

1 Answers1

1

You should use print instead of println. println prints a newline:

Prints to the standard output, with a newline. https://doc.rust-lang.org/std/macro.println.html

#![allow(unused_variables)]
#![allow(dead_code)]
#![allow(unused_mut)]

fn main()
{
    let mut x: isize;
    let mut y: isize; 

    y = 5; 

    for x in 0..=y
    {
        for _ in 0..=x
        {
            print!("*")
        }
        println!();
    }
}
JOSEFtw
  • 9,781
  • 9
  • 49
  • 67