Creating a Option<Vec<String>> from Option<String>

Hi guys,

I'm struggling to figure out an idiomatic way to create a one-element Option<Vec<String>> (for a rusoto_ec2::.DescribeLaunchTemplateVersionsRequest.versions) from a single
Option<String> (from a rusoto_autoscaling::LaunchTemplateSpecification.version)

At the moment I'm using this but it doesn't look great:

impl GetLaunchSetup for AutoScalingGroup {

  async fn get_launch_setup(&self) -> LaunchSetup {

    if let Some(lt) = self.launch_template.as_ref() {

      let client = Ec2Client::new(Region::UsEast1);
      let mut request = DescribeLaunchTemplateVersionsRequest::default();

      request.launch_template_id = lt.launch_template_id.clone();
      request.versions = Some(vec![lt.version.clone().unwrap()]); <-- *there must be a better way!*

      let response = client.describe_launch_template_versions(request).await.unwrap();

      if let Some(v) = response.launch_template_versions {
        version = v[0].version_number;
[...]

Any suggestion?

Thanks,
Chris

I suggest using:

request.versions = lt.version.clone().map(|s| vec![s]);

Additionally, the clone can be removed if you don't use lt.version after.

5 Likes

That looks good, thanks a lot!

Chris

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.