minus-squareshape_warrior_t@programming.devtoProgrammer Humor@programming.dev•Learning to program in rustlinkfedilinkEnglisharrow-up6·5 hours agoCan’t resist pointing out how you should actually write the function in a “real” scenario (but still not handling errors properly), in case anyone wants to know. If the list is guaranteed to have exactly two elements: fn is_second_num_positive_exact(input: &str) -> bool { let (_, n) = input.split_once(',').unwrap(); n.parse::<i32>().unwrap() > 0 } If you want to test the last element: fn is_last_num_positive(input: &str) -> bool { let n = input.split(',').next_back().unwrap(); n.parse::<i32>().unwrap() > 0 } If you want to test the 2nd (1-indexed) element: fn is_second_num_positive(input: &str) -> bool { let n = input.split(',').nth(1).unwrap(); n.parse::<i32>().unwrap() > 0 } linkfedilink
shape_warrior_t@programming.dev to Programming@programming.devEnglish · 2 days agoThis Overly Long Variable Name Could Have Been a Comment | Jonathan's Blogplus-squarejonathan-frere.comexternal-linkmessage-square40fedilinkarrow-up1104arrow-down15
arrow-up199arrow-down1external-linkThis Overly Long Variable Name Could Have Been a Comment | Jonathan's Blogplus-squarejonathan-frere.comshape_warrior_t@programming.dev to Programming@programming.devEnglish · 2 days agomessage-square40fedilink
minus-squareshape_warrior_t@programming.devtoProgrammer Humor@programming.dev•RFC 2119, the audiobooklinkfedilinkEnglisharrow-up2·3 days agoI can imagine Berdly Deltarune trying to explain this to Kris and Noelle in roughly this tone of voice. linkfedilink
Can’t resist pointing out how you should actually write the function in a “real” scenario (but still not handling errors properly), in case anyone wants to know.
If the list is guaranteed to have exactly two elements:
fn is_second_num_positive_exact(input: &str) -> bool { let (_, n) = input.split_once(',').unwrap(); n.parse::<i32>().unwrap() > 0 }
If you want to test the last element:
fn is_last_num_positive(input: &str) -> bool { let n = input.split(',').next_back().unwrap(); n.parse::<i32>().unwrap() > 0 }
If you want to test the 2nd (1-indexed) element:
fn is_second_num_positive(input: &str) -> bool { let n = input.split(',').nth(1).unwrap(); n.parse::<i32>().unwrap() > 0 }