re: Daily Challenge #12 - Next Larger Number VIEW POST

re: Took a long drive up for vacation yesterday so I wasn't able to get this types out! I think I know what I'm gonna implement so just gotta see if I ...

Got it done! Was able to write out what I was thinking last night without too much hassle, pretty happy with how it came out.

Could have tried not converting to chars in the middle, but it worked out pretty nicely still I think!

pub fn next_largest(n: u32) -> Option<u32> {
    let s = n.to_string();

    let mut chars: Vec<_> = s.chars().collect();

    for i in (0..chars.len() - 1).rev() {
        let first = chars[i].to_digit(10).unwrap();
        let second = chars[i + 1].to_digit(10).unwrap();

        if second > first {
            chars.swap(i, i + 1);
            let s: String = chars.iter().collect();

            return Some(s.parse().unwrap());


mod tests {
    use crate::*;

    fn it_works_for_examples_that_have_no_largest() {
        assert_eq!(next_largest(4), None);
        assert_eq!(next_largest(100), None);
        assert_eq!(next_largest(9876), None);

    fn it_works_for_the_examples() {
        assert_eq!(next_largest(12), Some(21));
        assert_eq!(next_largest(2019), Some(2091));
        assert_eq!(next_largest(513), Some(531));

    fn it_works_for_large_numebrs() {
        assert_eq!(next_largest(36852367), Some(36852376));
        assert_eq!(next_largest(123456789), Some(123456798));
        assert_eq!(next_largest(5010), Some(5100));
code of conduct - report abuse