How to consume data and return references to it?

I’m working on a very fast algorithm to solve the Advent of Code 2023 Day 8 part 2 challenge without the least-common-multiple approach.

One of my solutions works with HashMaps internally. As keys and values, they contain strings or references to strings that are present in the input data.

So basically, I’m taking apart the input data and spread it across a bunch of derived data structures.

I can now either copy these bits using .to_owned(), or just use references to sections within the original input, which brings runtime down from 2m12s to 1m38s.

My trouble is that I cannot use the fast reference approach without tying the lifetime of my data-structures to the lifetime of the reference to the input data, which means that the caller has to deal with that.

What I would prefer is to consume the input data, so the caller can forget about it, and still work with references internally. But that just doesn’t work out.

Let some dummy code highlight the issue:

use std::fs;

/**
 * Works fine, but needs to duplicate data, which makes it a bit slower
 */
pub fn read_file_and_analyze_little_slow(file_name: &str) -> (String, Vec<String>) {
    let content = fs::read_to_string(file_name).expect("Could not read the file you told me to analyze");
    let lines: Vec<String> = content.split("n").map(|x| { x.to_owned() }).collect();

    (content, lines)
}

/**
 * I wish I could do this, but the read content of the file is dropped
 * at the end of the file (or so the borrow checker thinks), invalidating
 * the references
 */
pub fn read_file_and_analyze_fast(file_name: &str) -> (String, Vec<&str>) {
    let content = fs::read_to_string(file_name).expect("Could not read the file you told me to analyze");
    let lines: Vec<&str> = content.split("n").collect();

    // Would need to make content live on somehow, so the references remain valid
    (content, lines) // Error: cannot move out of `content` because it is borrowed
}

/**
 * This works super well, but we've just transferred the issue to the calling function,
 * because now our result is tied to the lifetime of input string, meaning the parent
 * function can't pass it as _its_ result, unless it also works with lifetimes
 */
pub fn read_file_and_analyze_unwieldy<'a>(already_read_file: &'a str) -> Vec<&'a str> {
    already_read_file.split("n").collect()
}

I’m looking for solutions to make read_file_and_analyze_fast‘s API work. I suspect it’s probably possible but may require unsafe, which I haven’t worked with before.

This is a very interesting problem, so some general guidance to deal with issues like this one would also be appreciated

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật