【问题标题】:Rust lifetime scoping in structs结构中的 Rust 生命周期范围
【发布时间】:2020-04-20 06:00:43
【问题描述】:

所以,我正在努力将我用 Python 编写的字符串标记器移植到 Rust,但我遇到了一个我似乎无法解决生命周期和结构的问题。

所以,流程基本上是:

  1. 获取文件数组
  2. 将每个文件转换为 Vec<String> 的令牌
  3. 使用CounterUnicase 从每个vec 获取单个令牌实例的计数
  4. 将该计数与其他一些数据一起保存在结构中
  5. (未来)对结构集进行一些处理,以在每个文件数据旁边累积总数据
struct Corpus<'a> {
    words: Counter<UniCase<&'a String>>,
    parts: Vec<CorpusPart<'a>>
}

pub struct CorpusPart<'a> {
    percent_of_total: f32,
    word_count: usize,
    words: Counter<UniCase<&'a String>>
}

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 counted_words = collect(&tokens);

    CorpusPart {
        percent_of_total: 0.0,
        word_count: tokens.len(),
        words: counted_words
    }
}

pub fn tokenize(normalized: &mut String) -> Vec<String> {
    // snip ...
}

pub fn collect(results: &Vec<String>) -> Counter<UniCase<&'_ String>> {
    results.iter()
        .map(|w| UniCase::new(w))
        .collect::<Counter<_>>()
}

但是,当我尝试返回 CorpusPart 时,它抱怨它正在尝试引用局部变量 tokens。我该如何/应该如何处理这个问题?我尝试添加生命周期注释,但无法弄清楚...

基本上,我不再需要Vec&lt;String&gt;,但我确实需要其中一些用于柜台的String

感谢您的帮助,谢谢!

【问题讨论】:

  • 我们能否获得您的代码 sn-p 的几乎可编译版本(即唯一的错误是生命周期)?尽管乍一看,我相信您可以删除 CorpusPart 中的字符串引用(以及大多数其他字符串引用),因为您不再需要旧的 Vec。稍后我会发布正确的答案。

标签: rust lifetime-scoping


【解决方案1】:

这里的问题是你扔掉了Vec&lt;String&gt;,但仍然引用其中的元素。如果您不再需要Vec&lt;String&gt;,但仍需要其中的一些内容,则必须将所有权转移给其他东西。

我假设您希望 CorpusCorpusPart 都指向相同的字符串,因此您不会不必要地复制字符串。如果是这种情况,CorpusCorpusPart 必须拥有该字符串,以便不拥有该字符串的一方引用另一方拥有的字符串。 (听起来比实际复杂)

我会假设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&lt;a&gt; 别名为Vec&lt;a&gt;,因为我不知道您使用的是什么计数器。

Playground

【讨论】:

  • 这解决了我的问题,显然我还没有牢牢掌握所有权。非常感谢 into_iterlet length... 上有用的 cmets 为我澄清了很多事情。抱歉,我无法为您提供最小的代码示例,但您似乎明白了!
猜你喜欢
  • 1970-01-01
  • 2021-03-23
  • 2018-08-12
  • 1970-01-01
  • 2013-07-03
  • 1970-01-01
  • 2015-02-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多