The words Under construction in black text on a yellow background with diagonal black stipes surrounding it
I'm in the process of moving my site. It's still a work in progress. Please excuse the mess and broken links.

Convert A Vec Of Tuples Into A Vec Of Another Type In Rust

TODO: Pull subtitle into page object

Preface

This function takes in a vec of tuples that contain string slices that look like this:

Code
[("alfa", "bravo"), ("charlie", "delta")]

and converts it into a vec of string slices (pulled from the second tuple item) that looks like this:

Code
["bravo", "delta"]

Code

Here's the code:

Code
pub fn convert<'a>(
	vec_of_tuples: Vec<(&'a str, &'a str)>
	) -> Vec<&str> {
    vec_of_tuples.iter().map(|x| x.1).collect::<Vec<&str>>()
}

Testing

Here's the verification:

Code
#[cfg(test)]
mod test {
    use super::*;

    #[test]
    pub fn solo_test_convert() {
        let source: Vec<(&str, &str)> = 
		vec![("alfa", "bravo"), ("charlie", "delta")];
        let expected: Vec<&str> = 
		vec!["bravo", "delta"];
        assert_eq!(convert(source), expected);
    }
}

I've added the `::<<`` turbofish to `.collect()`` in the `convert`` funciton. Sometimes it's necessary, sometimes it's not. In this case, the code would work without it like this:

Code
// original 
vec_of_tuples.iter().map(|x| x.1).collect::<Vec<&str>>()

// updated
vec_of_tuples.iter().map(|x| x.1).collect()

I also added the type definitions to `source`` and `expected``
in the tests that might not be necessary. That code would look 
like:
Code
// original
let source: Vec<(&str, &str)> = 
	vec![("alfa", "bravo"), ("charlie", "delta")];
let expected: Vec<&str> = 
	vec!["bravo", "delta"];

// updated
let source = 
	vec![("alfa", "bravo"), ("charlie", "delta")];
let expected = 
	vec!["bravo", "delta"];

I'm not sure at what points those would be required and what points it's not so I defaulted to including them in the example.

Debugging Stuff

I'm moving stuff around right now. All this below is helping me figure out where to put stuff

        -- title

Convert A Vec Of Tuples Into A Vec Of Another Type In Rust

-- h2

Preface

This function takes in a vec of tuples that contain
string slices that look like this:

-- code
-- rust

[("alfa", "bravo"), ("charlie", "delta")]

-- p

and converts it into a vec of string slices (pulled
from the second tuple item) that looks like this:

-- code
-- rust

["bravo", "delta"]

-- h2

Code

Here's the code:

-- code
-- rust

pub fn convert<'a>(
	vec_of_tuples: Vec<(&'a str, &'a str)>
	) -> Vec<&str> {
    vec_of_tuples.iter().map(|x| x.1).collect::<Vec<&str>>()
}

-- h2

Testing

Here's the verification:

-- code
-- rust

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    pub fn solo_test_convert() {
        let source: Vec<(&str, &str)> = 
		vec![("alfa", "bravo"), ("charlie", "delta")];
        let expected: Vec<&str> = 
		vec!["bravo", "delta"];
        assert_eq!(convert(source), expected);
    }
}


-- p

I've added the `::<>`` turbofish to `.collect()`` in
the `convert`` funciton. Sometimes it's necessary, sometimes it's
not. In this case, the code would work without it like this:

-- code
-- rust

// original 
vec_of_tuples.iter().map(|x| x.1).collect::<Vec<&str>>()

// updated
vec_of_tuples.iter().map(|x| x.1).collect()

I also added the type definitions to `source`` and `expected``
in the tests that might not be necessary. That code would look 
like:

-- code
-- rust

// original
let source: Vec<(&str, &str)> = 
	vec![("alfa", "bravo"), ("charlie", "delta")];
let expected: Vec<&str> = 
	vec!["bravo", "delta"];

// updated
let source = 
	vec![("alfa", "bravo"), ("charlie", "delta")];
let expected = 
	vec!["bravo", "delta"];

-- p 

I'm not sure at what points those would be required and
what points it's not so I defaulted to including them
in the example.


-- categories
-- Rust 

-- metadata
-- date: 2023-06-25 15:50:29
-- id: 2riafnof
-- site: aws
-- type: post
-- status: published