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::rc::Rc;pub fn use_shared_data<T>(data: Rc<Vec<T>>) where T: std::fmt::Display, // 确保 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;use core::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!` let _ = data.iter().for_each(|element| println!("{}",element));}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;use std::fmt::Display;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>>),{ // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure let shared_numbers = Rc::new(items); take_item(Rc::clone(&shared_numbers)); take_item(Rc::clone(&shared_numbers)); take_item(Rc::clone(&shared_numbers));}// 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 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_data : Rc<Vec<String>> = Rc::new(items); take_item(Rc::clone(&rc_data)); take_item(Rc::clone(&rc_data)); 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>(data: Rc<Vec<T>>) // 1. Loop over each item in the vector and print it using `println!` 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 rc_items = Rc::new(items); for _ in 1..=3 { 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::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 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>>),{ // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure let rc_items = Rc::new(items); for _ in 1..=3 { 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::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.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 x = Rc::new(items); take_item(Rc::clone(&x)); take_item(Rc::clone(&x)); take_item(Rc::clone(&x));}// 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>>),{ // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure let rc = Rc::new(items); (0..3).into_iter().for_each(|_| { 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>>) { 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).into_iter().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: 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 x = Rc::new(items); for _ in 0..3 { take_item(Rc::clone(&x)); }}// 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 items = Rc::new(items); for _ in 1..=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::{Debug, Display}, ops::Deref, rc::Rc,};pub fn use_shared_data<T>(data: Rc<Vec<T>>)where T: Display + Debug,{ // 1. Loop over each item in the vector and print it using `println!` data.deref().iter().for_each(|i| 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::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 tt=Rc::new(items); take_item(Rc::clone(&tt)); take_item(Rc::clone(&tt)); take_item(Rc::clone(&tt));}// 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 line in data.iter() { println!("{}", line); }}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::fmt::Display;use std::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 my_rc = Rc::new(items); take_item(Rc::clone(&my_rc)); take_item(Rc::clone(&my_rc)); take_item(Rc::clone(&my_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::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 items_ref = Rc::new(items); take_item(Rc::clone(&items_ref)); take_item(Rc::clone(&items_ref)); take_item(Rc::clone(&items_ref));}// 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!` for o in data.iter() { println!("{}", o); }}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>>) { 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 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, 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 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::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 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;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_items = Rc::new(items); for _ in 0..3 { 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;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(|i| 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); for _ in 0..3 { 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;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>>),{ 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, 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(|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 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>(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(|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 r = Rc::new(items); for _ in 0..3 { 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;use std::fmt::{Display, Formatter};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(|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); // 2. Implement the function // Share the data as a reference-counted pointer 3 times with the closure 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);}