Rust
fn remove_first_and_last(string: &str) -> &str { remove_first_and_last_n_chars(&string, 1) } fn remove_first_and_last_n_chars(string: &str, chars_to_remove: usize) -> &str { if string.len() <= (2 * chars_to_remove) { panic!("Input string too short") } let start = chars_to_remove; let end = string.len() - 1 - chars_to_remove; &string[start..end] } fn main() { println!("Ans: {}", remove_first_and_last("Hello, world!")); println!("Ans: {}", remove_first_and_last_n_chars("Hello, world!", 2)); println!("Ans: {}", remove_first_and_last_n_chars("aa", 1)); println!("Ans: {}", remove_first_and_last_n_chars("aa", 2)); }
View it in the Rust Playground here: play.rust-lang.org/?version=stable...
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Rust
View it in the Rust Playground here: play.rust-lang.org/?version=stable...