코딩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/

Why are there no increment (++) and decrement (--) operators in Rust?

I've just started learning Rust, and it struck me as a bit odd that `x++` and `x--` aren't a part of the Rust language. I did some research, and I...

www.reddit.com





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

How to make a reverse ordered for loop?

Editor's note: This question was asked before Rust 1.0 was released and the .. "range" operator was introduced. The question's code no longer represents the current style, but some answers below uses

stackoverflow.com



반응형