How to use lifetimes properly in function that takes IntoIterator and return boxed iterator? : I've grasped the basics of rust lifetimes and how to work with iterators in it, but still have troubles to define a function that basically takes `iterable` and return another `iterable` based on the input `iterable` (doing some form of `map` for example)

(Yeah, I know that `iterable` doesn't mean anything in rust but I will still use it instead of IntoInterator)

Here is the gist of what I'd like to do. I want to take an iterable of tuples and return iterable of tuples also allocated on the heap.

    use std::iter::{once, repeat};
    
    
    fn foo<'a, I>(costs: I) -> Box<Iterator<Item = &'a (i32, f32)>>
        where I: IntoIterator<Item =  &'a (usize, f32)>,
        {
            let preliminary_examination_costs = once(10.0).chain(repeat(20.0));
            let id_assignment_costs = once(10.0).chain(repeat(20.0));
            let batch_fixed = once(10.0).chain(repeat(0.0));
            let temp: Vec<(usize, &(i32, f32))> = costs.into_iter().enumerate().collect();
            temp.sort_by_key(|&(_, &(n, cost))| {n});
            Box::new(temp.into_iter().map(
                |(i, &(n, cost))| {
                (
                    i, 
                    cost
                    + preliminary_examination_costs.next().unwrap()
                    + id_assignment_costs.next().unwrap()
                    + batch_fixed.next().unwrap()
                )
                }
            ))
        }

Here's the errors.

    error[E0277]: the trait bound `std::vec::Vec<(usize, &(i32, f32))>: std::iter::FromIterator<(usize, &'a (usize, f32))>` is not satisfied
      --> src/main.rs:10:77
       |
    10 |         let temp: Vec<(usize, &(i32, f32))> = costs.into_iter().enumerate().collect();
       |                                                                             ^^^^^^^ a collection of type `std::vec::Vec<(usize, &(i32, f32))>` cannot be built from an iterator over elements of type `(usize, &'a (usize, f32))`
       |
       = help: the trait `std::iter::FromIterator<(usize, &'a (usize, f32))>` is not implemented for `std::vec::Vec<(usize, &(i32, f32))>`
    
    error[E0271]: type mismatch resolving `<[closure@src/main.rs:13:13: 21:14 preliminary_examination_costs:_, id_assignment_costs:_, batch_fixed:_] as std::ops::FnOnce<((usize, &(i32, f32)),)>>::Output == &(i32, f32)`
      --> src/main.rs:12:9
       |
    12 | /         Box::new(temp.into_iter().map(
    13 | |             |(i, &(n, cost))| {
    14 | |             (
    15 | |                 i, 
    ...  |
    21 | |             }
    22 | |         ))
       | |__________^ expected tuple, found &(i32, f32)
       |
       = note: expected type `(usize, f32)`
                  found type `&(i32, f32)`
       = note: required because of the requirements on the impl of `std::iter::Iterator` for `std::iter::Map<std::vec::IntoIter<(usize, &(i32, f32))>, [closure@src/main.rs:13:13: 21:14 preliminary_examination_costs:_, id_assignment_costs:_, batch_fixed:_]>`
       = note: required for the cast to the object type `std::iter::Iterator<Item=&(i32, f32)>`


https://play.rust-lang.org/?gist=b2bef2a57f76aae6c70eb0e28f4db44f&version=stable