Move History

Rooted by: Last Char
Fork Selected
  • Strings
    Data Types
    Description

    Take any string and return the last character.

    If the string is empty, return nothing.

    Code
    fn last_char(string: &str) -> Option<char> {
        string.chars().rev().next()
    }
    Test Cases
    #[test]
    fn test_last_char() {
        assert_eq!(last_char(""), None);
        assert_eq!(last_char("Hello, world"), Some('d'));
        assert_eq!(last_char("こんにちは"), Some('は'));
    }
  • Code
    • function lastChar(string) {
    • return string[string.length - 1];
    • fn last_char(string: &str) -> Option<char> {
    • string.chars().rev().next()
    • }
    Preloaded Code
    • // is this the place where you can build your module and export it ?
    Test Cases
    • describe("basic test", function(){
    • var domain='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
    • lC=(t)=>t.slice(-1);
    • const randomString=(length, chars)=>{
    • var result = '';
    • for (var i = length; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)];
    • return result;
    • }
    • var rString=randomString(Math.floor(Math.random() * (domain.length - 1 + 1)) +1,domain)
    • console.log(rString,lastChar(rString))
    • it ("Test passed",()=>{
    • Test.assertEquals(lastChar(rString), lC(rString), 'Test passed')
    • });
    • });
    • #[test]
    • fn test_last_char() {
    • assert_eq!(last_char(""), None);
    • assert_eq!(last_char("Hello, world"), Some('d'));
    • assert_eq!(last_char("こんにちは"), Some('は'));
    • }