rust - 如何在线程中借值而不是移动
问题描述
我有两个线程都需要对 Vec 进行不可变访问,目前我必须克隆该向量以便两个线程都可以使用数据,我想尝试摆脱这个额外的副本
use clap::{App, Arg};
use std::fs;
use std::thread;
mod part1;
mod part2;
fn main() {
let matches = App::new("day1")
.about("AoC2018 day1")
.arg(
Arg::with_name("INPUT")
.help("The file to read input from")
.required(true)
.index(1),
)
.get_matches();
let filename = matches.value_of("INPUT").unwrap();
let file_content = fs::read_to_string(filename).expect("Could not read/open file");
let numbers: Vec<i64> = file_content
.lines()
.map(|x| x.parse::<i64>().expect("Could not extract value"))
.collect();
let numbers2 = numbers.clone();
let handle1 = thread::spawn(move || part1::part1(&numbers));
let handle2 = thread::spawn(move || part2::part2(&numbers2));
let result1 = handle1.join();
let result2 = handle2.join();
println!("Part 1: {}", result1.unwrap());
println!("Part 2: {}", result2.unwrap());
}
解决方案
推荐阅读
- python - 从 Python 到 Arduino 的串行通信故障排除
- c++ - Delete the row in 2d vector
- typescript - 如何修复 foreach 循环中无法读取属性错误
- sql - Django SQL 查询计数
- python - 在比较 Python 中的两个数组时做一些动作
- php - 禁止:您无权访问此服务器上的 /< - Windows
- angular - 一些引导功能在 Angular 中不起作用,但在常规 HTML 文件中起作用
- perl - 在单行 perl 中查看正则表达式
- python - 如何在python中解析url数组?
- reactjs - 快照测试意外令牌