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::collections::HashMap; | |
fn main() -> Result<(), String> { | |
let numbers = vec![1, 2, 3, 4, 5, 6]; | |
// This creates the scope for the threads | |
let (median, mode) = std::thread::scope(|scope| { | |
// This scoped thread calculates average | |
let median_thread = |
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::collections::HashMap; | |
fn main() -> Result<(), String> { | |
let numbers = vec![1, 2, 3, 4, 5, 6]; | |
// This thread calculates average | |
let median_thread = | |
std::thread::spawn(|| numbers.iter().sum::<i32>() as f32 / numbers.len() as f32); | |
// This thread calculates mode |
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
impl Add<i32> for &Counter { | |
type Output = Result<i32, String>; | |
fn add(self, rhs: i32) -> Self::Output { | |
let sum = self.0 + rhs; | |
if sum > MAX_COUNTER { | |
Err("Counter overflow".to_string()) | |
} else { | |
Ok(i32::try_from(sum).unwrap()) | |
} |
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
impl Add<i16> for &Counter { | |
type Output = i32; | |
fn add(self, rhs: i16) -> Self::Output { | |
self.0 + i32::from(rhs) | |
} | |
} |
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
impl Add for &Counter { | |
type Output = Counter; | |
fn add(self, rhs: Self) -> Self::Output { | |
Counter(self.0 + rhs.0) | |
} | |
} |
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
impl Add for Counter { | |
type Output = Counter; | |
fn add(self, rhs: Self) -> Self::Output { | |
Self(self.0 + rhs.0) | |
} | |
} |
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
pub trait Add<Rhs = Self> { | |
type Output; | |
fn add(self, rhs: Rhs) -> Self::Output; | |
} |
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
mod mul; // Now it's hidden too | |
pub use mul::mul; // But we can still access this function | |
mod strings; | |
pub fn hello() -> &'static str { | |
strings::HELLO | |
} |
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
pub mod mul; | |
mod strings; // Hidden to the outside world | |
pub fn hello() -> &'static str { | |
strings::HELLO | |
} |
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
mod calc; | |
mod foo; | |
fn main() { | |
assert!(foo::bar()); | |
assert_eq!(2, calc::mul::mul(2, calc::mul::MUL_IDENTITY)); | |
println!("{}", calc::hello()); | |
} |