Here's the context for the ruby code:
ec2.keys.sort.each do |type| {
instances=ec2[type];
delta = ((instances.length - reservations[type]) rescue 0);
message += "`#{type}`: *need_reservation=#{delta}* running=#{instances.length} reserved=#{reservations[type]} instances=_#{instances.to_s}_\n\n\n"
}
context for the rust code I'm trying to write:
ec2.keys().sorted().for_each(|r#type|{
let instances = &ec2[r#type];
let delta = instances.len() as u64 - reservations.get(&r#type.as_str()).expect("mismatched types");
let instances_str: String = instances.iter().map(ToString::to_string).collect();
message += format!("`{}`: *need_reservation={}* running={} reserved={} instances=_{}_\n\n\n",
r#type, delta, instances.len(), reservations[r#type], instances_str);
});
I have a couple questions. for the line with:
let delta = instances.len() as u64 - reservations.get(&r#type.as_str()).expect("mismatched types");
I was only able to get it to compile as written, and &reservations[r#type] would not work for me the way it worked for ec2 in the first line. I was wondering why this is the case. Also is using ".expect" the equivalent for "rescue 0"?
My main problem, however, is in this formatted string line:
message += format!("`{}`: *need_reservation={}* running={} reserved={} instances=_{}_\n\n\n",
r#type, delta, instances.len(), reservations[r#type], instances_str);
The way I understood the format function is that you can put any type into the corresponding "{}". However, I am getting errors with "reservation[#type] saying "the trait Borrow<&std::string::String>
is not implemented for &str
". I tried replacing it with:
reservations.get(&r#type.as_str())
But that gives the error "std::option::Option<&u64>cannot be formatted with the default formatter". Is there a different format function that would make this work? Finally, the "instances_str" right after reservations also gives me an error saying "expected
&str, found struct
std::string::String`. That's why I tried
let instances_str: String = instances.iter().map(ToString::to_string).collect();
and
instances.to_string()
But I still keep getting the same error. For reference, instances is a vector and reservations is a HashMap with key of type String and value of type u64.
Any help would be greatly appreciated.