Last active
May 16, 2019 01:53
-
-
Save zsytssk/9aeac8776043ec40b3179c28deadb0cc to your computer and use it in GitHub Desktop.
rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::sync::{Arc, Mutex}; | |
use std::thread; | |
#[derive(Debug)] | |
struct Test<T> { | |
arr: Vec<T>, | |
len: u8, | |
} | |
impl<T> Test<T> { | |
fn new() -> Test<T> { | |
Test { | |
arr: vec![], | |
len: 0, | |
} | |
} | |
fn push(&mut self, item: T) { | |
self.arr.push(item); | |
self.len += 1; | |
} | |
} | |
impl Test<u8> { | |
fn sum(&mut self) -> u8 { | |
self.arr.iter().sum() | |
} | |
} | |
fn main() { | |
// let mut test: Test<u8> = ; | |
let v = Arc::new(Mutex::new(Test::<u8>::new())); | |
let mut handles = vec![]; | |
for i in 0..3 { | |
let clone_v = v.clone(); | |
let handle = thread::spawn(move || { | |
clone_v.lock().unwrap().push(i * 3); | |
println!("{}", clone_v.lock().unwrap().sum()); | |
}); | |
handles.push(handle); | |
} | |
for handle in handles { | |
handle.join().unwrap(); | |
} | |
println!("{:?}", v); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::sync::Mutex; | |
fn main() { | |
let v = Mutex::new(vec![]); | |
let p = &v; | |
p.lock().unwrap().push(2); | |
test_fn(&v); | |
println!("{:?}", v); | |
} | |
fn test_fn(v: &Mutex<Vec<i32>>) { | |
v.lock().unwrap().push(1); | |
v.lock().unwrap().push(1); | |
v.lock().unwrap().push(1); | |
} | |
// eg: error | |
// fn main() { | |
// let mut v = vec![]; | |
// let p = &mut v; | |
// p.push(2); | |
// test_fn(&mut v); | |
// } | |
// fn test_fn(v: &mut Vec<i32>) { | |
// v.push(1); | |
// v.push(1); | |
// v.push(1); | |
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let result1 = match divide(2.0, 0.0) { | |
Some(x) => x, | |
None => std::f64::INFINITY, | |
}; | |
println!("{:?}", result1); | |
let result2 = divide(2.0, 0.0).unwrap(); | |
println!("{:?}", result2); | |
} | |
fn divide(numerator: f64, denominator: f64) -> Option<f64> { | |
if denominator == 0.0 { | |
None | |
} else { | |
Some(numerator / denominator) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[derive(Debug)] | |
enum Version { | |
Version1, | |
Version2, | |
} | |
fn parse_version(header: &[u8]) -> Result<Version, &'static str> { | |
match header.get(0) { | |
None => Err("invalid header length"), | |
Some(&1) => Ok(Version::Version1), | |
Some(&2) => Ok(Version::Version2), | |
Some(_) => Err("invalid version"), | |
} | |
} | |
fn main() { | |
let version = parse_version(&[1, 2, 3, 4]); | |
match version { | |
Ok(v) => println!("working with version: {:?}", v), | |
Err(e) => println!("Err parse header: {:?}", e), | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::sync::{Arc, Mutex}; | |
use std::thread; | |
struct MyInner { | |
s: u8, | |
} | |
struct My { | |
inner: Arc<Mutex<MyInner>>, | |
} | |
impl My { | |
fn new(s: u8) -> My { | |
My { | |
inner: Arc::new(Mutex::new(MyInner { s })), | |
} | |
} | |
fn start(&mut self) { | |
let local_self = self.inner.clone(); | |
thread::spawn(move || { | |
local_self.lock().unwrap().add(); | |
}); | |
} | |
fn get(&self) -> u8 { | |
self.inner.lock().unwrap().s | |
} | |
} | |
impl MyInner { | |
fn add(&mut self) { | |
println!("{:?}", self.s); | |
self.s += 1; | |
println!("{:?}", self.s); | |
} | |
} | |
fn main() { | |
let mut thread_test = My::new(0); | |
loop { | |
thread_test.start(); | |
println!("{}", thread_test.get()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::sync::{Arc, Mutex}; | |
use std::thread; | |
fn main() { | |
let v = Arc::new(Mutex::new(vec![])); | |
let mut handles = vec![]; | |
for i in 0..3 { | |
let clone_v = v.clone(); | |
let handle = thread::spawn(move || { | |
clone_v.lock().unwrap().push(i); | |
}); | |
handles.push(handle); | |
} | |
for handle in handles { | |
handle.join().unwrap(); | |
} | |
println!("{:?}", v); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment