Rust programming video tutorial (Advanced) – 017_ 2 messaging 2

Time:2021-9-12

Video address

Headline address:https://www.ixigua.com/i677586170644791348…
Station B address:https://www.bilibili.com/video/av81202308/

Source address

GitHub address:https://github.com/anonymousGiga/learn_rus…

Explanation content

1. Channel and ownership transfer
(1) Example:

use std::thread;
use std::sync::mpsc;

fn main() {
    let (tx, rx) = mpsc::channel();

    thread::spawn(move || {
        let val = String::from("hi");
        tx.send(val).unwrap();
        println! ("val is {}", val);// Error, Val cannot be used here because the ownership of Val has been moved to the channel
    });

    let received = rx.recv().unwrap();
    println!("Got: {}", received);
}

2. Example of sending multiple values

use std::thread;
use std::sync::mpsc;
use std::time::Duration;

fn main() {
    let (tx, rx) = mpsc::channel();

    thread::spawn(move || {
        let vals = vec![
            String::from("hi"),
            String::from("from"),
            String::from("the"),
            String::from("thread"),
        ];

        for val in vals {
            tx.send(val).unwrap();
            thread::sleep(Duration::from_secs(1));
        }
    });

    for received in rx {
        println!("Got: {}", received);
    }
}

This work adoptsCC agreement, reprint must indicate the author and the link to this article

Linghu rushed

Recommended Today

P3120 [USACO15FEB]Cow Hopscotch G

portal ideas A simple idea is a\(O(n^2m^2)\)transfer of: \[f_{i,j}=\sum_{x=1}^{i-1}\sum_{y=1}^{j-1}f_{x,y}*[a_{i,j}!=a_{x,y}] \] There are so many constraints, thinking about using cdq divide and conquer to optimize we consider theRowdivide and conquer\([l,mid]\) Then brute force enumerationList,use\([l,mid]\)to update\((mid,r]\) enumerate columns each time\(j\)After the transfer, the\(\sum_{i=l}^{mid}f_{i,j}\)add to\(sum\), and update\(s[a_{i,j}]\)(used to subtract the contribution of the same number) The equation for […]