- I'm new to Rust.
- I'm reading SHA1-as-hex-strings from a file - a lot of them, approx. 30 million.
- In the text file, they are sorted ascending numerically.
- I want to be able to search the list, as fast as possible.
- I (think I) want to read them into a (sorted)
Vec<
primitive_type::U256>
for fast searching.
So, I've tried:
log("Loading haystack.");
// total_lines read earlier
let mut the_stack = Vec::<primitive_types::U256>::with_capacity(total_lines);
if let Ok(hay) = read_lines(haystack) { // Get BufRead
for line in hay { // Iterate over lines
if let Ok(hash) = line {
the_stack.push(U256::from(hash));
}
}
}
log(format!("Read {} hashes.", the_stack.len()));
The error is:
$ cargo build
Compiling nsrl v0.1.0 (/my_app)
error[E0277]: the trait bound `primitive_types::U256: std::convert::From<std::string::String>` is not satisfied
--> src/main.rs:55:24
|
55 | the_stack.push(U256::from(hash));
| ^^^^^^^^^^ the trait `std::convert::From<std::string::String>` is not implemented for `primitive_types::U256`
|
= help: the following implementations were found:
<primitive_types::U256 as std::convert::From<&'a [u8; 32]>>
<primitive_types::U256 as std::convert::From<&'a [u8]>>
<primitive_types::U256 as std::convert::From<&'a primitive_types::U256>>
<primitive_types::U256 as std::convert::From<&'static str>>
and 14 others
= note: required by `std::convert::From::from`
This code works if instead of the variable hash
I have a string literal, e.g. "123abc"
.
I think I should be able to use the implementation std::convert::From<&'static str>
, but I don't understand how I'm meant to keep hash
in scope?
I feel like what I'm trying to achieve is a pretty normal use case:
- Iterate over the lines in a file.
- Add the line to a vector.
What am I missing?