코딩Coding/★Rust★기초★Syntax
Rust❤️ increment(++) decrement(--)하는 법
내인생PLUS
2022. 6. 10. 17:48
728x90
https://www.reddit.com/r/rust/comments/jf66eu/why_are_there_no_increment_and_decrement/
increment(++)
for i in 0..1000 {
println!("{:?}", i);
}
decrement(--)하는 법
for i in (0..1000).rev() {
println!("{:?}", i);
}
예시
use rayon::prelude::*;
use std::time::{Duration, Instant};
fn increment_all(input: &mut [i32]) {
input.par_iter_mut().for_each(|p| *p += 1);
for i in (0..1000).rev() {
println!("{:?}", i);
}
}
fn main() {
let now_1 = Instant::now();
let now_2 = Instant::now();
print!("thread1 : ");
print!("{:?}", increment_all(&mut [10]));
let elapsed1= now_1.elapsed().as_millis();
print!("thread2 : ");
print!("{:?}", increment_all(&mut [10]));
let elapsed2 = now_2.elapsed().as_millis();
println!("1nd thread elapse : {elapsed1} millsec");
println!("2nd thread elapse : {elapsed2} millsec");
}
결과
2
1
0
()1nd thread elapse : 10 millsec
2nd thread elapse : 10 millsec
https://stackoverflow.com/questions/25170091/how-to-make-a-reverse-ordered-for-loop
반응형