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>>) { // 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);}
use std::{fmt::Display, rc::Rc};pub fn use_shared_data<T: Display>(data: Rc<Vec<T>>) { data.iter().for_each(|e| println!("{e}"));}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); 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>>) { 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 rc = Rc::new(items); for _ in 0..3 { 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>(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 items = Rc::new(items); take_item(items.clone()); take_item(items.clone()); // This clone is actually not needed, stupid bad exercise! 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!` 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 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::{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!` data.iter().for_each(|val| println!("{}", val));}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_vector = Rc::new(items); take_item(Rc::clone(&rc_vector)); take_item(Rc::clone(&rc_vector)); take_item(Rc::clone(&rc_vector));}// 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!` 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 // Share the input as a reference-counted pointer 3 times with the given closure. // Do cheap clones only and avoid deep copying the data. 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;pub fn use_shared_data<T: std::fmt::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 items = Rc::new(items); take_item(Rc::clone(&items)); take_item(Rc::clone(&items)); take_item(Rc::clone(&items));}// Example usagepub fn main() { 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}"); }}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>),{ let items = Rc::new(items); take_item(Rc::clone(&items)); take_item(Rc::clone(&items)); take_item(Rc::clone(&items));}// Example usagepub fn main() { 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 x in &*data { 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 items = Rc::new(items); for _ 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::fmt::Display;use std::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>>),{ // 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!` 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(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!` 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(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>(data: Rc<Vec<T>>)where T: std::fmt::Display,{ // 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>>),{ // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure let rc = Rc::new(items); (0..=2).for_each(|_| { // println!("Current ref count is: {}", Rc::strong_count(&rc)); // Will always print 1, because the reference is dropped after the execution of take_item 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)); // This would work also // use_shared_data(shared_numbers.clone()); // 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!` 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); (0..3).for_each(|_| 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;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!` 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()); 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;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!` 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>(data: Rc<Vec<T>>) where T : std::fmt::Display,{ // 1. Loop over each item in the vector and print it using `println!` data.iter().for_each(|e| println!("{}",*e));}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 p = Rc::new(items); take_item(Rc::clone(&p)); take_item(Rc::clone(&p)); take_item(Rc::clone(&p));}// 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 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>(data: Rc<Vec<T>>)where T: Display,{ 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_items = Rc::new(items); (0..=2).for_each(|_| take_item(rc_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;pub fn use_shared_data<T: std::fmt::Display>(data: Rc<Vec<T>>) { 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 = 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 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::{Debug, Display}, rc::Rc,};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 p1 = Rc::new(items); take_item(p1.clone()); take_item(p1.clone()); take_item(p1.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: Display>(data: Rc<Vec<T>>) { // 1. Loop over each item in the vector and print it using `println!` data.iter().for_each(|d| 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 = 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::{fmt::Display, rc::Rc};pub fn use_shared_data<T: Display>(data: Rc<Vec<T>>) { for element in data.iter() { println!("{element}") }}pub fn share_data_to_other_functions<F>(mut take_item: F, items: Vec<String>)where F: FnMut(Rc<Vec<String>>) { 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;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>>),{ 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>(data: Rc<Vec<T>>)where T: std::fmt::Display{ // 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>>),{ // 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(shared_items.clone()); take_item(shared_items.clone()); take_item(shared_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;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 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;pub fn use_shared_data(data: Rc<Vec<impl std::fmt::Display>>) { // 1. Loop over each item in the vector and print it using `println!` for v in data.iter() { println!("{v}"); }}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); for _ in 0..3 { 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;pub fn use_shared_data(data: Rc<Vec<impl std::fmt::Display>>) { // 1. Loop over each item in the vector and print it using `println!` for datum in data.iter() { println!("{}", datum); }}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 _ 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);}