【问题标题】:How does one pass a Vec<T> to a function when the trait Seek is required? [duplicate]当需要特征 Seek 时,如何将 Vec<T> 传递给函数? [复制]
【发布时间】:2018-11-05 00:51:58
【问题描述】:

我有一个 API,我需要将 Vec&lt;u8&gt; 传递给它需要其参数来实现 std::io::Seek

fn some_func<T: Seek + Write>(foo: &mut T) {/* body */}

板条箱作者建议在这里使用File,但是我想避免在这里使用它,因为它会导致不必要的文件创建。 Vec&lt;u8&gt; 满足 Write 特征,但不满足 Seek 特征。有什么办法可以避免在这里使用File

【问题讨论】:

    标签: generics rust traits


    【解决方案1】:

    您可以将Vec&lt;u8&gt; 包装在std::io::Cursor 中:

    let mut buf: Cursor<Vec<u8>> = Cursor::new(Vec::new());
    some_func(&mut buf);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多