

by Sam Jaques @
fn sum_of_squares(input: &[i32]) -> i32 {
input.iter()
.map(|i| i * i)
.sum()
}
fn sum_of_squares(input: &[i32]) -> i32 {
input.iter()
.map(|i| i * i)
.sum()
}
use rayon::prelude::*;
fn sum_of_squares(input: &[i32]) -> i32 {
input.par_iter()
.map(|i| i * i)
.sum()
}
Do not communicate by sharing memory; instead, share memory by communicating
fn main() {
std::thread::spawn(|| {
let val = String::from("hi");
});
}
use std::sync::mpsc;
fn main() {
let (tx, rx) = mpsc::channel();
std::thread::spawn(|| {
let value = String::from("hi");
});
}
use std::sync::mpsc;
fn main() {
let (tx, rx) = mpsc::channel();
std::thread::spawn(move || { // move tx to new thread
let value = String::from("hi");
tx.send(value).unwrap();
});
let received = rx.recv().unwrap();
println!("Got: {}", received);
}
async fn announce(src: IpAddr, msg: &[u8]) -> Result<()> {
let socket: tokio::net::UdpSocket = create_socket(src)?;
loop {
socket
.send_to(msg, AES_MULTICAST_SOCK)
.await
.expect("Failed to send SAP packet");
time::sleep(SAP_ANNOUNCE_PERIOD).await;
}
}
async fn announce(src: IpAddr, msg: &[u8]) -> Result<()> {
let socket: tokio::net::UdpSocket = create_socket(src)?;
loop {
socket
.send_to(msg, AES_MULTICAST_SOCK)
.await
.expect("Failed to send SAP packet");
time::sleep(SAP_ANNOUNCE_PERIOD).await;
}
}
let json = include_bytes!("config.json");
let config = serde_json::from_str::<Config>(
&String::from_utf8_lossy(json)
);
#[given(expr = "a tokbox session is active")]
async fn session_ongoing(w: &mut World) {
let session_info = tokbox_helper::create_session().await;
REDIS.publish_session_info(session_info.clone());
w.session = Some(session_info);
}
#[then(regex = "the stream is (not |un)?published")]
async fn stream_published(w: &mut World, published: String) {
let published = published.is_empty();
w.expected_streams = published as u8;
check_stream_published(w, published).await
}
match x {
1 => println!("one"),
2 | 3 => println!("two or three"),
4..=9 => println!("within range"),
x if x > 10 => println!("more than ten: {}", x),
x => println!("{}", x), // OR next line
_ => println!("default Case")
}
match x {
1 => println!("one"),
2 | 3 => println!("two or three"),
4..=9 => println!("within range"),
x if x > 10 => println!("more than ten: {}", x),
x => println!("{}", x), // OR next line
_ => println!("default Case")
}
function map<T, U>(array: T[], trans: (item: T) => U): U[]
{ ... }
fn map<B, F>(self, f: F) -> Map<Self, F>
where
Self: Sized,
F: FnMut(Self::Item) -> B,
{ ... }
Questions? thoughts?
email me @ sam.jaques@tmceurope.com