Map

Function Map 

Source
pub fn Map(mapping: fn(rune) -> rune, s: impl AsRef<[byte]>) -> Vec<byte> 
Expand description

Map returns a copy of the string s with all its characters modified according to the mapping function. If mapping returns a negative value, the character is dropped from the string with no replacement.

zh-cn 将s的每一个unicode码值r都替换为mapping(r),返回这些新码值组成的字符串拷贝。如果mapping返回一个负值,将会丢弃该码值而不会被替换。(返回值中对应位置将没有码值)

§Example

use gostd_bytes as bytes;

   let rot13 = |r: u32| -> u32 {
       if r >= 'A' as u32 && r < 'Z' as u32 {
           return 'A' as u32 + (r - 'A' as u32 + 13) % 26;
       }
       if r >= 'a' as u32 && r <= 'z' as u32 {
           return 'a' as u32 + (r - 'a' as u32 + 13) % 26;
       }
       r
   };
   let s = "'Twas brillig and the slithy gopher...".as_bytes();
   assert_eq!(
       "'Gjnf oevyyvt naq gur fyvgul tbcure...".as_bytes(),
       bytes::Map(rot13, s)
   );