-
StringsData 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 fn test_last_char() { assert_eq!(last_char(""), None); assert_eq!(last_char("Hello, world"), Some('d')); assert_eq!(last_char("こんにちは"), Some('は')); }
Output:
-
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('は'));
- }
- All
- {{group.name}} ({{group.count}})
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}