For loop is waiting for input before iterating [duplicate] - rust

This question already has answers here:
Why does this read input before printing?
(3 answers)
How do I print output without a trailing newline in Rust?
(2 answers)
Closed 1 year ago.
I'm just learning Rust, and when I run the below code, it waits for my input before iterating through the for loop. Why does this happen, and how can I stop it?
pub fn whatever() {
for i in 1..121 {
print!("{} ", i);
}
let mut input = String::new();
std::io::stdin().read_line(&mut input).unwrap();
let n: i32 = input.trim().parse().unwrap();
}

Related

How do I push a string with a variable into a vector? [duplicate]

This question already has answers here:
How to create a formatted String out of a literal in Rust?
(2 answers)
How can I append a formatted string to an existing String?
(1 answer)
Closed 1 year ago.
I'm trying to solve a Kata where I'm asked to "count sheep". A function count_sheep is called with a positive integer as parameter. I'm supposed to return a string with a murmur: "1 sheep...2 sheep...3 sheep..."
This is what I have so far:
fn count_sheep(n: u32) -> String {
let mut Sheep_vec = String::new();
let mut i = 1;
while i <= n {
sheep_string = ("{} sheep..", i);
Sheep_vec.push(sheep_string);
i = i + 1;
}
Sheep_vec
}
I wanted to return the string analog to the println! macro, but that fails, because .push() doesn't support multiple argumentss.

concat string using format!() and return to &'static str [duplicate]

This question already has answers here:
Return local String as a slice (&str)
(7 answers)
How to concatenate static strings in Rust
(3 answers)
Closed 2 years ago.
This post was edited and submitted for review 10 months ago and failed to reopen the post:
Duplicate This question has been answered, is not unique, and doesn’t differentiate itself from another question.
I'll get error when compiled: 'returns value referencing data owned by the current function'
here's the code:
fn return_static_str(a: &'static str, b: &'static str) -> &'static str {
format!("{} {}", a,b).as_str()
}
fn main() {
let s = return_static_str("Hello ", "World!");
}
using return as_ref() get same issue.

how can I convert a string to a vector in rust? [duplicate]

This question already has answers here:
How do I split a string in Rust?
(7 answers)
Closed 2 years ago.
I would like to receive a:
"Hello, I would like to meet you"
and convert to:
["Hello,", "I", "would", "like", "to", "meet", "you"]
how could I make that?
There is a dedicated method for str to do this called split(). You can then use collect() to put the result in a Vec.
In your case:
fn main() {
let original = "Hello, I would like to meet you";
let split = original.split(' ');
let vec: Vec<_> = split.collect();
println!("{:?}", vec);
}

How to write string representation of a u32 into a String buffer without extra allocation? [duplicate]

This question already has answers here:
How to efficiently push displayable item into String? [duplicate]
(1 answer)
How can I append a formatted string to an existing String?
(1 answer)
Closed 3 years ago.
I can convert a u32 into String and append it to an existing string buffer like this
let mut a = String::new();
let b = 1_u32.to_string();
a.push_str(&b[..]);
But this involves allocation of a new string object in the heap.
How to push the string representation of a u32 without allocating a new String?
Should I implement an int-to-string function from scratch?
Use write! and its family:
use std::fmt::Write;
fn main() {
let mut foo = "answer ".to_string();
write!(&mut foo, "is {}.", 42).expect("This shouldn't fail");
println!("The {}", foo);
}
This prints The answer is 42., and does exactly one allocation (the explicit to_string).
(Permalink to the playground)

Initialize array with consecutive integers [duplicate]

This question already has answers here:
How can I initialize an array using a function? [duplicate]
(5 answers)
How do I collect into an array?
(10 answers)
Does Rust have a way to apply a function/method to each element in an array or vector?
(2 answers)
Closed 3 years ago.
The community reviewed whether to reopen this question last month and left it closed:
Original close reason(s) were not resolved
What is the simplest form of initializing an array with consecutive integers from 0 to N?
I have this code, but I think idiomatic Rust will look much simpler:
const NUM: u32 = 8;
fn main() {
let mut int_list: [u32; NUM as usize] = [0; NUM as usize];
for i in 0..NUM {
int_list[i as usize] = i;
}
println!("data: {:?}", int_list);
}
playground

Resources