Rust 线程demo


fn main()
{
    let mut fuck = vec![100,200,300];

    for i in &mut fuck
    {
        *i += 20;
    }
    println!("{:?}", fuck);

}

use std::collections::HashMap;

fn main()
{
    let mut score = HashMap::new();
    score.insert(String::from("fuck"), 10);
    score.insert("dick".to_string(), 30);

    // for (key, val) in &score  {
    //     // println!("{}:{}", key,val);
    // }
    println!("{:?}", &score);
}

#![allow(unused)]
fn main() {
    use std::collections::HashMap;

    let text = "hello world wonderful world";

    let mut map = HashMap::new();

    for word in text.split_whitespace() {
        let count = map.entry(word).or_insert(0);
        *count += 1;
    }

    println!("{:?}", map);
}

#![allow(unused)]
fn main()
{
    use std::collections::HashMap;
    let text = "fuck world fuck you";
    let mut map = HashMap::new();

    for w in text.split_whitespace()
    {
        let count = map.entry(w).or_insert(0);
        *count += 1;
    }
    println!("{:?}", map);
}

use windows::Win32::UI::Accessibility::Value_Value_Property_GUID;

pub struct Guess
{
    value :i32,
}

impl Guess {
    pub fn new(mut value:i32) -> Guess
    {
        if value < 1 || value > 100
        {
            // panic!("dick");
            value = 1
        }
        Guess {
            value
        }
    }
    pub fn value(&self) -> i32
    {
        self.value
    }
}

fn main()
{
    let k = Guess::new(1000);
    println!("{}",k.value);
}



fn main() {
    thread::spawn(|| {
        for i in 1..10 {
            println!("hi number {} from the spawned thread!", i);
            thread::sleep(Duration::from_millis(1));
        }
    });

    for i in 1..5 {
        println!("hi number {} from the main thread!", i);
        thread::sleep(Duration::from_millis(1));
    }
}

fn main() {
   let fuck =  thread::spawn(|| {
        for i in 0..10 {
            println!("hi number {} from the spawned thread!", i);
            thread::sleep(Duration::from_millis(1));
        }
    });

    for i in 0..5 {
        println!("hi number {} from the main thread!", i);
        thread::sleep(Duration::from_millis(1));
    }
    
}

use std::{sync::mpsc, thread, time::Duration};
fn main()
{
    let (tx, rc) = mpsc::channel();

    let tx1 = tx.clone() ;
    thread::spawn(move|| {
        let s = vec!["fuck","me","happ"];
        for i in s {
            tx.send(i).unwrap();
            thread::sleep(Duration::from_millis(1));
        }

    });

    thread::spawn(move|| {
        let ss = vec!["dick","my","dick"];
        for i in ss {
            tx1.send(i).unwrap();
            thread::sleep(Duration::from_millis(1));
        }

    });

    for r in rc {
        println!("got {}", r);
    }
}

use std::{ sync::Mutex};
fn main()
{
    let m = Mutex::new(5);
    {
        let mut num = m.lock().unwrap();
        *num = 30;
    }
    println!("{:?}", m);

}


use std::sync::{Mutex, Arc};
use std::thread;
fn main()
{
    let counter = Arc::new(Mutex::new(0));
    let mut handles = vec![];
    for _ in 0..10 {
        let counter = Arc::clone(&counter);
        let handler = thread::spawn(move || {
            let mut num = counter.lock().unwrap();
            *num += 1 ;
        });
        handles.push(handler);
    }

    for hand in handles {
        hand.join().unwrap();
    }

    println!("result={}", *counter.lock().unwrap());


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值