Проблема здесь в том, что вы отбрасываете Vec<String>
, но все еще ссылаетесь на элементы внутри него. Если вам больше не нужен Vec<String>
, но все же требуется некоторое содержимое внутри, вы должны передать право собственности на что-то другое.
Я предполагаю, что вы хотите, чтобы Corpus
и CorpusPart
оба указывали на одинаковые строки, поэтому вы не должны дублировать строки без необходимости. Если это так, то Corpus
или CorpusPart
должны владеть строкой, чтобы тот, у которого нет строки, ссылался на строки, принадлежащие другой. (Звучит сложнее, чем на самом деле)
Я предполагаю, что CorpusPart
владеет строкой, а Corpus
просто указывает на эти строки
use std::fs::DirEntry;
use std::fs::read_to_string;
pub struct UniCase<a> {
test: a
}
impl<a> UniCase<a> {
fn new(item: a) -> UniCase<a> {
UniCase {
test: item
}
}
}
type Counter<a> = Vec<a>;
struct Corpus<'a> {
words: Counter<UniCase<&'a String>>, // Will reference the strings in CorpusPart (I assume you implemented this elsewhere)
parts: Vec<CorpusPart>
}
pub struct CorpusPart {
percent_of_total: f32,
word_count: usize,
words: Counter<UniCase<String>> // Has ownership of the strings
}
fn process_file(entry: &DirEntry) -> CorpusPart {
let mut contents = read_to_string(entry.path())
.expect("Could not load contents.");
let tokens = tokenize(&mut contents);
let length = tokens.len(); // Cache the length, as tokens will no longer be valid once passed to collect
let counted_words = collect(tokens);
CorpusPart {
percent_of_total: 0.0,
word_count: length,
words: counted_words
}
}
pub fn tokenize(normalized: &mut String) -> Vec<String> {
Vec::new()
}
pub fn collect(results: Vec<String>) -> Counter<UniCase<String>> {
results.into_iter() // Use into_iter() to consume the Vec that is passed in, and take ownership of the internal items
.map(|w| UniCase::new(w))
.collect::<Counter<_>>()
}
Я связал Counter<a>
с Vec<a>
, так как я не знаю, какой счетчик вы используете.
Детская площадка