Axl
Axl

Reputation: 8502

Rust "cannot return value referencing temporary value" static ref Mutex Vec slice

How to return a slice of a static Mutex<Vec>? Obviously I thought adding "static" to &[i64] would suffice. I've seen solutions with .iter().skip().take().collect() and .iter().map().collect() however those don't appear to compile either. Would prefer to return a slice (&[T]) rather than returning a vector (Vec<T>). Thanks!

use lazy_static::lazy_static;
use std::sync::{Mutex, MutexGuard};
use std::vec::Vec;

const CAPACITY: usize = 1048576;

lazy_static! {
    static ref integer_keys: Mutex<Vec<[usize; 2]>> = Mutex::new(vec![[0, 0]; CAPACITY + 1]);
    static ref integer_values: Mutex<Vec<i64>> = Mutex::new(Vec::with_capacity(CAPACITY + 1));
}

pub fn integers_get(n: u64) -> &'static [i64] {
    let keys = integer_keys.lock().unwrap();
    return &integer_values.lock().unwrap()[keys[n as usize][0]..keys[n as usize][1]];
}

Upvotes: 0

Views: 33

Answers (0)

Related Questions