Reference-counted pointers, Rc<T>, are a type of smart pointer in Rust that allow multiple ownership of the same data. This means you can have multiple references to a value, and the value will only be dropped when the last reference is out of scope. This is particularly useful in scenarios where you want to share data between multiple parts of your program without needing to copy it.
In this challenge, you'll use Rc<T> to share data between functions.
Implement the functions use_shared_data and share_data_to_other_functions to work with Rc<T>.
use_shared_data:
Rc<Vec<T>> as argument.println!.share_data_to_other_functions:
If you're stuck, here are some hints to help you solve the challenge:
Rc::new to wrap your vector in an Rc smart pointer.Rc::clone to create new references to the shared data.use_shared_data, make sure to use T: Display to allow printing elements with {} formatting.use std::{fmt::Display, rc::Rc};pub fn use_shared_data<T: Display>(data: Rc<Vec<T>>) { for item in data.iter() { println!("{}", item); }}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ let rc = Rc::new(items); take_item(Rc::clone(&rc)); take_item(Rc::clone(&rc)); take_item(Rc::clone(&rc));}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::rc::Rc;pub fn use_shared_data<T>(data: Rc<Vec<T>>)where T: std::fmt::Display{ // 1. Loop over each item in the vector and print it using `println!` for item in data.iter() { println!("{}", item); }}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure let items = Rc::new(items); for _i in 0..3 { take_item(Rc::clone(&items)); }}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::rc::Rc;use std::fmt::Display;pub fn use_shared_data<T: Display>(data: Rc<Vec<T>>) { // 1. Loop over each item in the vector and print it using `println!` for i in data.iter() { println!("{}", i) }}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ let t = Rc::new(items); take_item(Rc::clone(&t)); take_item(Rc::clone(&t)); take_item(Rc::clone(&t)); // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::rc::Rc;use std::fmt::Display;pub fn use_shared_data<T: Display>(data: Rc<Vec<T>>) { // 1. Loop over each item in the vector and print it using `println!` for i in data.iter() { println!("{}", i) }}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ let t = Rc::new(items); take_item(Rc::clone(&t)); take_item(Rc::clone(&t)); take_item(Rc::clone(&t)); // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::{fmt::Display, rc::Rc};pub fn use_shared_data<T>(data: Rc<Vec<T>>)where T: Display,{ data.iter().for_each(|p| println!("{}", p));}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ let shared = Rc::new(items); take_item(Rc::clone(&shared)); take_item(Rc::clone(&shared)); take_item(Rc::clone(&shared));}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::rc::Rc;pub fn use_shared_data<T:std::fmt::Display>(data: Rc<Vec<T>>) { // 1. Loop over each item in the vector and print it using `println!` for each_data in data.iter(){ println!("{}",each_data); }}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure let data = Rc::new(items); take_item(Rc::clone(&data)); take_item(Rc::clone(&data)); take_item(Rc::clone(&data));}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::rc::Rc;pub fn use_shared_data<T: std::fmt::Display>(data: Rc<Vec<T>>) { // 1. Loop over each item in the vector and print it using `println!` for item in data.iter() { println!("{}", item); }}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure let r = Rc::new(items); take_item(Rc::clone(&r)); take_item(Rc::clone(&r)); take_item(Rc::clone(&r));}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::rc::Rc;pub fn use_shared_data<T>(data: Rc<Vec<T>>)where T: std::fmt::Display,{ // 1. Loop over each item in the vector and print it using `println!` for item in data.iter() { println!{"{}", item}; }}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure let reference = Rc::new(items); take_item(Rc::clone(&reference)); take_item(Rc::clone(&reference)); take_item(Rc::clone(&reference));}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::rc::Rc;pub fn use_shared_data<T>(data: Rc<Vec<T>>) // 1. Loop over each item in the vector and print it using `println!` where T: std::fmt::Display, { for data in data.iter() { println!("{}", data); } }pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>), { let rc_items = Rc::new(items); take_item(Rc::clone(&rc_items)); take_item(Rc::clone(&rc_items)); take_item(Rc::clone(&rc_items)); // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::rc::Rc;pub fn use_shared_data<T>(data: Rc<Vec<T>>)where T: std::fmt::Display,{ for data in data.iter() { println!("{}", data); }}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ let rc_items = Rc::new(items); take_item(Rc::clone(&rc_items)); take_item(Rc::clone(&rc_items)); take_item(Rc::clone(&rc_items));}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::{fmt::Display, rc::Rc};pub fn use_shared_data<T: Display>(data: Rc<Vec<T>>) { data.iter().for_each(|x| println!("{}", x));}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ let vec = Rc::new(items); take_item(Rc::clone(&vec)); take_item(Rc::clone(&vec)); take_item(Rc::clone(&vec));}pub fn main() { let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::rc::Rc;use std::fmt::Display;pub fn use_shared_data<T: Display>(data: Rc<Vec<T>>) { // 1. Loop over each item in the vector and print it using `println!` for i in data.iter() { println!("{}", i); }}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure let vec = Rc::new(items); take_item(Rc::clone(&vec)); take_item(Rc::clone(&vec)); take_item(Rc::clone(&vec));}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::rc::Rc;use std::fmt::Display;pub fn use_shared_data<T>(data: Rc<Vec<T>>) where T:Display + std::fmt::Debug{ // 1. Loop over each item in the vector and print it using `println! for i in data.iter() { println!("{}",i); }}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure let items_rc = Rc::new(items); take_item(Rc::clone(&items_rc)); take_item(Rc::clone(&items_rc)); take_item(Rc::clone(&items_rc));}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::rc::Rc;use std::fmt::Display;pub fn use_shared_data<T>(data: Rc<Vec<T>>)where T:Display, { // 1. Loop over each item in the vector and print it using `println!` for i in data.iter(){ println!("{}",i); }}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure let reference_data=Rc::new(items); for _ in 0..3 { take_item(Rc::clone(&reference_data)) }}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::rc::Rc;use std::fmt::Display;pub fn use_shared_data<T>(data: Rc<Vec<T>>)where T: Display { data.iter().for_each(|x| println!("{}", x));}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>) where F: FnMut(Rc<Vec<String>>),{ let shared_item = Rc::new(items); for _ in 0..3 { take_item(Rc::clone(&shared_item)); }}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::rc::Rc;use std::fmt::Display;pub fn use_shared_data<T>(data: Rc<Vec<T>>)where T: Display,{ // 1. Loop over each item in the vector and print it using `println!` for d in data.iter() { println!("{}", d); }}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure let shared_item = Rc::new(items); for _ in 0..3 { take_item(Rc::clone(&shared_item)); }}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::rc::Rc;use std::fmt::Display;pub fn use_shared_data<T>(data: Rc<Vec<T>>)where T:Display, { // 1. Loop over each item in the vector and print it using `println!` for i in data.iter(){ println!("{}",i); }}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure let reference_data=Rc::new(items); for _ in 0..3 { take_item(Rc::clone(&reference_data)) }}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::rc::Rc;pub fn use_shared_data<T: std::fmt::Display>(data: Rc<Vec<T>>) { // 1. Loop over each item in the vector and print it using `println!` for d in data.iter() { println!("{}", d); }}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure let rc_data = Rc::new(items); for _ in 0..3 { take_item(Rc::clone(&rc_data)); }}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::rc::Rc;use std::fmt::Display;pub fn use_shared_data<T: Display>(data: Rc<Vec<T>>) { // 1. Loop over each item in the vector and print it using `println!` for d in data.iter(){ println!("{d}"); }}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closurei let item = Rc::new(items); for _ in 0..3{ take_item(item.clone()); } }// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::{fmt::Display, rc::Rc};pub fn use_shared_data<T: Display>(data: Rc<Vec<T>>) { for item in data.iter() { println!("{}", item); }}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ let shared_data = Rc::new(items); for _ in 0..3 { take_item(shared_data.clone()); }}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::{fmt::Display, rc::Rc};pub fn use_shared_data<T: Display>(data: Rc<Vec<T>>) { for item in data.iter() { println!("{}", item); }}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ let shared_data = Rc::new(items); for _ in 0..3 { take_item(shared_data.clone()); }}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::rc::Rc;pub fn use_shared_data<T>(data: Rc<Vec<T>>) where T : std::fmt::Display,{ // 1. Loop over each item in the vector and print it using `println!` for item in data.iter() { println!("{}",item); }}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure let shared_data = Rc::new(items); for _ in 0..3 { take_item(shared_data.clone()); } }// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::rc::Rc;use std::fmt::Display;pub fn use_shared_data<T>(data: Rc<Vec<T>>) where T: Display{ // 1. Loop over each item in the vector and print it using `println!` for item in &*data { println!("{item}"); }}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure let shared_nums = Rc::new(items); for _ in 0..3 { take_item(Rc::clone(&shared_nums)); }}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::rc::Rc;pub fn use_shared_data<T: std::fmt::Debug + std::fmt::Display>(data: Rc<Vec<T>>) { // 1. Loop over each item in the vector and print it using `println!` for i in data.iter() { println!("{}", i) }}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure let the_items = Rc::new(items); take_item(the_items.clone()); take_item(the_items.clone()); take_item(the_items.clone());}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::rc::Rc;use std::fmt::Display;pub fn use_shared_data<T>(data: Rc<Vec<T>>) where T : Display{ for item in data.iter(){ println!("{}",item); }}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure let ref_count = Rc::new(items); take_item(ref_count.clone()); take_item(ref_count.clone()); take_item(ref_count.clone());}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::{rc::Rc, fmt::Display};pub fn use_shared_data<T>(data: Rc<Vec<T>>)where T: Display{ // 1. Loop over each item in the vector and print it using `println!` for item in data.iter() { println!("{item}"); }}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure let items = Rc::new(items); take_item(items.clone()); take_item(items.clone()); take_item(items.clone());}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::fmt::Display;use std::rc::Rc;pub fn use_shared_data<T: Display>(data: Rc<Vec<T>>) { // 1. Loop over each item in the vector and print it using `println!` for i in data.iter() { println!("{}", i); }}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure let rc = Rc::new(items); take_item(rc.clone()); take_item(rc.clone()); take_item(rc.clone());}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::fmt::Display;use std::rc::Rc;pub fn use_shared_data<T: Display>(data: Rc<Vec<T>>) { for e in data.iter() { println!("{e}"); }}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ let rc = Rc::new(items); take_item(rc.clone()); take_item(rc.clone()); take_item(rc.clone());}use std::rc::Rc;use std::fmt::Display;pub fn use_shared_data<T: Display>(data: Rc<Vec<T>>) { // 1. Loop over each item in the vector and print it using `println!` for item in data.iter() { println!("{}", item); }}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure let shared_data = Rc::new(items); take_item(Rc::clone(&shared_data)); take_item(Rc::clone(&shared_data)); take_item(Rc::clone(&shared_data));}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::{ fmt::Display, rc::Rc };pub fn use_shared_data<T: Display>(data: Rc<Vec<T>>) { // 1. Loop over each item in the vector and print it using `println!` for item in data.iter() { println!("{}", item); }}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>) where F: FnMut(Rc<Vec<String>>){ // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure let shared_data = Rc::new(items); take_item(Rc::clone(&shared_data)); take_item(Rc::clone(&shared_data)); take_item(Rc::clone(&shared_data));}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::rc::Rc;pub fn use_shared_data<T: std::fmt::Display>(data: Rc<Vec<T>>) { // 1. Loop over each item in the vector and print it using `println!` data.iter().for_each(|item| println!("{}", item))}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure let items = Rc::new(items); (0..3).for_each(|_| take_item(items.clone()));}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::rc::Rc;use std::fmt::Display;pub fn use_shared_data<T: Display>(data: Rc<Vec<T>>) { data.iter().for_each(|x|{ println!("{}",x); } );}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure let rc = Rc::new(items); take_item( Rc::clone(&rc)); take_item( Rc::clone(&rc)); take_item( Rc::clone(&rc));}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::rc::Rc;pub fn use_shared_data<T: std::fmt::Display>(data: Rc<Vec<T>>) { // 1. Loop over each item in the vector and print it using `println!` data.iter().for_each(|a| println!("{}", a));}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure let item = Rc::new(items); (0..3).for_each(|_| take_item(Rc::clone(&item)));}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::rc::Rc;pub fn use_shared_data<T: std::fmt::Display>(data: Rc<Vec<T>>) { for i in data.iter(){ println!("{}", i); }}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ let val = Rc::new(items); for _i in 0..3 { take_item(val.clone()); }}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::{fmt::Display, rc::Rc};pub fn use_shared_data<T>(data: Rc<Vec<T>>)where T: Display,{ // 1. Loop over each item in the vector and print it using `println!` data.iter().for_each(|value| println!("{value}"));}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure let items = Rc::new(items); take_item(Rc::clone(&items)); take_item(Rc::clone(&items)); take_item(Rc::clone(&items));}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::rc::Rc;use std::fmt::Display;pub fn use_shared_data<T: Display>(data: Rc<Vec<T>>) { // 1. Loop over each item in the vector and print it using `println!` for elt in data.iter() { println!("{}", &elt); }}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure let data = Rc::new(items); for _ in 0..3 { take_item(Rc::clone(&data)); }}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::rc::Rc;pub fn use_shared_data<T: std::fmt::Display>(data: Rc<Vec<T>>) { // 1. Loop over each item in the vector and print it using `println!` data.clone().iter().for_each(|item| println!("{item}"));}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure let data = Rc::new(items); take_item(Rc::clone(&data)); take_item(Rc::clone(&data)); take_item(Rc::clone(&data));}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::rc::Rc;pub fn use_shared_data<T: std::fmt::Display>(data: Rc<Vec<T>>) { for item in data.iter() { println!("{}", item); } // 1. Loop over each item in the vector and print it using `println!`}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ let shared_items = Rc::new(items); take_item(Rc::clone(&shared_items)); take_item(Rc::clone(&shared_items)); take_item(Rc::clone(&shared_items)); // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::rc::Rc;use std::fmt::Display;pub fn use_shared_data<T: Display>(data: Rc<Vec<T>>) { // 1. Loop over each item in the vector and print it using `println!` for i in data.iter() { println!("{}", i); }}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure let shared_items = Rc::new(items); take_item(Rc::clone(&shared_items)); take_item(Rc::clone(&shared_items)); take_item(Rc::clone(&shared_items));}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::{fmt::Display, rc::Rc};pub fn use_shared_data<T: Display>(data: Rc<Vec<T>>) { data.iter().for_each(|item| { println!("{}", item); });}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ let rc = Rc::new(items); take_item(rc.clone()); take_item(rc.clone()); take_item(rc.clone());}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::{fmt::Display, rc::Rc};pub fn use_shared_data<T: Display>(data: Rc<Vec<T>>) { // 1. Loop over each item in the vector and print it using `println!` for item in data.as_ref() { println!("{}", item); }}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure let rc = Rc::new(items); take_item(rc.clone()); take_item(rc.clone()); take_item(rc.clone());}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::rc::Rc;use std::fmt::Display;pub fn use_shared_data<T: Display>(data: Rc<Vec<T>>) { data.iter().for_each(|item| println!("{item}"));}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ let shared = Rc::new(items); take_item(Rc::clone(&shared)); take_item(Rc::clone(&shared)); take_item(Rc::clone(&shared));}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::rc::Rc;use std::fmt::Display;pub fn use_shared_data<T: Display>(data: Rc<Vec<T>>) { // 1. Loop over each item in the vector and print it using `println!` for d in data.iter() { println!("{}", d); }}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure let reference = Rc::new(items); take_item(Rc::clone(&reference)); take_item(Rc::clone(&reference)); take_item(Rc::clone(&reference));}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::{fmt::Display, rc::Rc};pub fn use_shared_data<T: Display>(data: Rc<Vec<T>>) { data.iter().for_each(|el| println!("{}", el));}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ let shared_items = Rc::new(items); take_item(Rc::clone(&shared_items)); take_item(Rc::clone(&shared_items)); take_item(Rc::clone(&shared_items));}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::rc::Rc;use std::fmt::Display;pub fn use_shared_data<T: Display>(data: Rc<Vec<T>>) { // 1. Loop over each item in the vector and print it using `println!` for d in data.iter() { println!("{}", d); }}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure let reference = Rc::new(items); take_item(reference.clone()); take_item(reference.clone()); take_item(reference.clone());}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::rc::Rc;use std::fmt::Display;pub fn use_shared_data<T: Display>(data: Rc<Vec<T>>) { // 1. Loop over each item in the vector and print it using `println!` for d in &*data { println!("{d}"); }}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure let reference = Rc::new(items); take_item(reference.clone()); take_item(reference.clone()); take_item(reference.clone()); }// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::rc::Rc;use std::fmt::Display;pub fn use_shared_data<T: Display>(data: Rc<Vec<T>>) { data .iter() .for_each(|item| println!("{}", item))}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ let shared = Rc::new(items); for _ in 0..3 { take_item(shared.clone()) }}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::rc::Rc;pub fn use_shared_data<T: std::fmt::Display>(data: Rc<Vec<T>>) { for d in &*data { println!("{}", d); } // 1. Loop over each item in the vector and print it using `println!`}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ let reference = Rc::new(items); take_item(reference.clone()); take_item(reference.clone()); take_item(reference.clone()); // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::fmt::Display;use std::rc::Rc;pub fn use_shared_data<T>(data: Rc<Vec<T>>)where T: Display,{ // 1. Loop over each item in the vector and print it using `println!` data.iter().for_each(|item| println!("{}", item));}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure let reference = Rc::new(items); take_item(reference.clone()); take_item(reference.clone()); take_item(reference.clone());}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}use std::rc::Rc;pub fn use_shared_data<T: std::fmt::Display>(data: Rc<Vec<T>>) { // 1. Loop over each item in the vector and print it using `println!` data.iter().for_each(|x| println!("{}", *x));}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ let shared = Rc::new(items); take_item(Rc::clone(&shared)); take_item(Rc::clone(&shared)); take_item(Rc::clone(&shared)); // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure}// Example usagepub fn main() { // Example usage of `use_shared_data` let shared_numbers = Rc::new(vec![1, 2, 3, 4, 5]); println!("Using shared data:"); use_shared_data(Rc::clone(&shared_numbers)); // Example usage of `share_data_to_other_functions` let strings = vec!["Rust".to_string(), "is".to_string(), "awesome!".to_string()]; let print_data = |data: Rc<Vec<String>>| { println!("Printing shared data:"); for item in data.iter() { println!("{}", item); } }; println!("\nSharing data with other functions:"); share_data_to_other_functions(print_data, strings);}