小编ERn*_*sTL的帖子

无法将字符串拆分为具有显式生存期的字符串切片,因为字符串的活动时间不够长

我正在写一个应该从实现BufRead特性的东西中读取的库; 网络数据流,标准输入等.第一个函数应该从该读取器读取数据单元并返回一个填充的结构,该结构主要填充有&'a str从线路中的帧解析的值.

这是一个最小版本:

mod mymod {
    use std::io::prelude::*;
    use std::io;

    pub fn parse_frame<'a, T>(mut reader: T)
    where
        T: BufRead,
    {
        for line in reader.by_ref().lines() {
            let line = line.expect("reading header line");
            if line.len() == 0 {
                // got empty line; done with header
                break;
            }
            // split line
            let splitted = line.splitn(2, ':');
            let line_parts: Vec<&'a str> = splitted.collect();

            println!("{} has value {}", line_parts[0], line_parts[1]);
        }
        // more reads down here, therefore the reader.by_ref() above …
Run Code Online (Sandbox Code Playgroud)

rust

0
推荐指数
1
解决办法
553
查看次数

标签 统计

rust ×1