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());
}
Rust 线程demo
最新推荐文章于 2024-09-17 16:04:46 发布