大家好,我遇到了一个问题——完全缺乏工作教程。我发现的 inkwell (llvm) 和 Cranelift 都是 5-6 年前无法使用的矿渣。不知何故,神奇的是,对于作者(例如)模块来说,它是一种结构,而对我来说,它是一种特质。
实际上,这就是为什么我要求您帮助我找到使用上述库之一的教程或新示例。
大家好,我遇到了一个问题——完全缺乏工作教程。我发现的 inkwell (llvm) 和 Cranelift 都是 5-6 年前无法使用的矿渣。不知何故,神奇的是,对于作者(例如)模块来说,它是一种结构,而对我来说,它是一种特质。
实际上,这就是为什么我要求您帮助我找到使用上述库之一的教程或新示例。
刚刚开始使用 Rust 中的线程。我无法在两个线程之间传递哈希表。如何正确地做到这一点以及我的错误在哪里?任务是在一个线程中收集哈希表并在另一个线程中打印它。
use std::thread;
use std::time::Duration;
use std::fs;
use std::io::{stdin, Read};
use std::collections::HashMap;
fn main() {
let mut hello = String::from("Hello, ");
let mut influx_data:HashMap<String, i32> = HashMap::new();
//second thread
thread::spawn(move || {
let mut character = [0];
hello = String::from("sasasas ");
while let Ok(_) = stdin().read(&mut character) {
if character[0] == b'\n' {
influx_data.entry(hello).and_modify(|count| *count += 1).or_insert(1);
println!("counter_bss cnt=7\n");
}
}
thread::sleep(Duration::from_secs(1));
});
//main thread
loop {
let data = "Some data!";
fs::write("/tmp/foo", data).expect("Unable to write file");
for (key, value) in &influx_data {
println!("{} {}", key, value);
}
thread::sleep(Duration::from_secs(10));
}
}
错误
|
23 | influx_data.entry(hello).and_modify(|count| *count += 1).or_insert(1);
| ^^^^^ value moved here, in previous iteration of loop
use std::collections::HashMap;
pub fn frequency(input: &[&str], worker_count: usize) -> HashMap<char, usize> {
let (tx, rx) = std::sync::mpsc::channel();
let mut hndls = Vec::new();
{
// вычисляем количество потоков и размер порции
let mut portion = 10; //минимальная порция
let len = input.len();
let mut cnt = (len as f64 / portion as f64 + 0.5) as usize;
cnt = if cnt > worker_count {cnt} else {worker_count};
portion = (len as f64 / cnt as f64 + 0.5) as usize;
for (i, chank) in input.chunks(portion).enumerate() {
let tx_local = tx.clone();
let chank_local = chank.clone();
let handl = std::thread::spawn(move || {
let mut mp_local = HashMap::new();
//mp_local.insert('a', 1);
for s in chank_local {
for ch in s.to_lowercase().chars() {
if let Some(val) = mp_local.get_mut(&ch) {
*val += 1;
} else {
mp_local.insert(ch, 1);
}
}
}
if let Err(e) = tx_local.send(mp_local) {
eprintln!("Ошибка записи в канал потоком {i}: {e}");
}
});
hndls.push(handl);
}
}
drop(tx);
let mut mp = HashMap::new();
for mp_local in rx {
for (k,v) in mp_local {
if let Some(val) = mp.get_mut(&k) {
*val += v;
} else {
mp.insert(k, v);
}
}
}
for handl in hndls {
handl.join().unwrap();
}
mp
}
错误:
error[E0521]: borrowed data escapes outside of function
--> src/lib.rs:19:25
|
3 | pub fn frequency(input: &[&str], worker_count: usize) -> HashMap<char, usize> {
| ----- - let's call the lifetime of this reference `'1`
| |
| `input` is a reference that is only valid in the function body
...
19 | let handl = std::thread::spawn(move || {
| _________________________^
20 | | let mut mp_local = HashMap::new();
21 | |
22 | | //mp_local.insert('a', 1);
... |
35 | | }
36 | | });
| | ^
| | |
| |______________`input` escapes the function body here
| argument requires that `'1` must outlive `'static`
error[E0521]: borrowed data escapes outside of function
--> src/lib.rs:19:25
|
3 | pub fn frequency(input: &[&str], worker_count: usize) -> HashMap<char, usize> {
| ----- - let's call the lifetime of this reference `'2`
| |
| `input` is a reference that is only valid in the function body
...
19 | let handl = std::thread::spawn(move || {
| _________________________^
20 | | let mut mp_local = HashMap::new();
21 | |
22 | | //mp_local.insert('a', 1);
... |
35 | | }
36 | | });
| | ^
| | |
| |______________`input` escapes the function body here
| argument requires that `'2` must outlive `'static`
For more information about this error, try `rustc --explain E0521`.
error: could not compile `parallel-letter-frequency` (lib test) due to 2 previous errors
warning: build failed, waiting for other jobs to finish...
error: could not compile `parallel-letter-frequency` (lib) due to 2 previous errors
我在这里读了一本成长书,我不明白它们有什么不同i32
…… u32
。
对这个主题给出一个广泛的答案,最好用你自己的话,而不是链接到相关章节。
谢谢
找不到rand依赖项
Cargo.toml
[package]
name = "guessing_game"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rand = "0.4.0"
尝试了不同的 rand = "0.4.0" 和 rand = "3.1.4"
写道:
C:\Dima\Rust\guessing_game1>cargo run
Updating crates.io index
Compiling winapi v0.3.9
error[E0432]: unresolved import `shared::basetsd`
--> C:\Users\comp\.cargo\registry\src\index.crates.io-6f17d22bba15001f\winapi-0.3.9\src\shared\minwindef.rs:8:13
|
8 | use shared::basetsd::{LONG_PTR, UINT_PTR};
| ^^^^^^^ could not find `basetsd` in `shared`
error[E0432]: unresolved import `shared::ntdef`
--> C:\Users\comp\.cargo\registry\src\index.crates.io-6f17d22bba15001f\winapi-0.3.9\src\shared\minwindef.rs:9:13
|
9 | use shared::ntdef::{HANDLE, LONG};
| ^^^^^ could not find `ntdef` in `shared`
error[E0432]: unresolved import `shared::basetsd`
--> C:\Users\comp\.cargo\registry\src\index.crates.io-6f17d22bba15001f\winapi-0.3.9\src\um\ntsecapi.rs:7:13
|
7 | use shared::basetsd::{ULONG64, ULONG_PTR};
| ^^^^^^^ could not find `basetsd` in `shared`
error[E0432]: unresolved import `shared::ntdef`
--> C:\Users\comp\.cargo\registry\src\index.crates.io-6f17d22bba15001f\winapi-0.3.9\src\um\ntsecapi.rs:10:13
|
10 | use shared::ntdef::NTSTATUS;
| ^^^^^ could not find `ntdef` in `shared`
error[E0432]: unresolved import `shared::sspi`
--> C:\Users\comp\.cargo\registry\src\index.crates.io-6f17d22bba15001f\winapi-0.3.9\src\um\ntsecapi.rs:11:13
|
11 | use shared::sspi::SecHandle;
| ^^^^ could not find `sspi` in `shared`
error[E0432]: unresolved import `um::lsalookup`
--> C:\Users\comp\.cargo\registry\src\index.crates.io-6f17d22bba15001f\winapi-0.3.9\src\um\ntsecapi.rs:12:9
|
12 | use um::lsalookup::{
| ^^^^^^^^^ could not find `lsalookup` in `um`
error[E0432]: unresolved import `um::subauth`
--> C:\Users\comp\.cargo\registry\src\index.crates.io-6f17d22bba15001f\winapi-0.3.9\src\um\ntsecapi.rs:15:9
|
15 | use um::subauth::{PUNICODE_STRING, STRING, UNICODE_STRING};
| ^^^^^^^ could not find `subauth` in `um`
error[E0432]: unresolved import `shared::basetsd`
--> C:\Users\comp\.cargo\registry\src\index.crates.io-6f17d22bba15001f\winapi-0.3.9\src\um\winnt.rs:9:13
|
9 | use shared::basetsd::{
| ^^^^^^^ could not find `basetsd` in `shared`
error[E0432]: unresolved import `shared::ktmtypes`
--> C:\Users\comp\.cargo\registry\src\index.crates.io-6f17d22bba15001f\winapi-0.3.9\src\um\winnt.rs:13:13
|
13 | use shared::ktmtypes::UOW;
| ^^^^^^^^ could not find `ktmtypes` in `shared`
error[E0432]: unresolved import `vc::excpt`
--> C:\Users\comp\.cargo\registry\src\index.crates.io-6f17d22bba15001f\winapi-0.3.9\src\um\winnt.rs:17:9
|
17 | use vc::excpt::EXCEPTION_DISPOSITION;
| ^^^^^ could not find `excpt` in `vc`
error[E0432]: unresolved import `vc::vcruntime`
--> C:\Users\comp\.cargo\registry\src\index.crates.io-6f17d22bba15001f\winapi-0.3.9\src\um\winnt.rs:18:9
|
18 | use vc::vcruntime::size_t;
| ^^^^^^^^^ could not find `vcruntime` in `vc`
error[E0432]: unresolved import `shared::ntdef`
--> C:\Users\comp\.cargo\registry\src\index.crates.io-6f17d22bba15001f\winapi-0.3.9\src\um\winnt.rs:151:17
|
151 | pub use shared::ntdef::LARGE_INTEGER;
| ^^^^^ could not find `ntdef` in `shared`
error[E0432]: unresolved import `shared::ntdef`
--> C:\Users\comp\.cargo\registry\src\index.crates.io-6f17d22bba15001f\winapi-0.3.9\src\um\winnt.rs:153:17
|
153 | pub use shared::ntdef::ULARGE_INTEGER;
| ^^^^^ could not find `ntdef` in `shared`
error[E0432]: unresolved import `shared::ntdef`
--> C:\Users\comp\.cargo\registry\src\index.crates.io-6f17d22bba15001f\winapi-0.3.9\src\um\winnt.rs:159:17
|
159 | pub use shared::ntdef::LUID;
| ^^^^^ could not find `ntdef` in `shared`
For more information about this error, try `rustc --explain E0432`.
error: could not compile `winapi` (lib) due to 14 previous errors
C:\Dima\Rust\guessing_game1>
我尝试手动添加兰特
C:\Dima\Rust\guessing_game1>cargo add rand
Updating crates.io index
Adding rand v0.4.0 to dependencies.
Features as of v0.4.1:
+ libc
+ std
- alloc
- i128_support
- nightly
C:\Dima\Rust\guessing_game1>cargo add alloc
Updating crates.io index
error: the crate `alloc` could not be found in registry index.
C:\Dima\Rust\guessing_game1>cargo add i128_support
Updating crates.io index
error: the crate `i128_support` could not be found in registry index.
C:\Dima\Rust\guessing_game1>cargo add nightly
Updating crates.io index
error: the crate `nightly` could not be found in registry index.
C:\Dima\Rust\guessing_game1>cargo add rand
Updating crates.io index
Adding rand v0.4.0 to dependencies.
Features as of v0.4.1:
+ libc
+ std
- alloc
- i128_support
- nightly
但由于某种原因它不支持库。
这是设置