Skip to main content

encode

Function encode 

Source
pub fn encode(text: &str) -> Result<String, MorseError>
Expand description

Encodes ASCII text as Morse code.

Letters are converted to uppercase before lookup. Individual letter codes are joined by a single space; words (separated by ASCII spaces in the input) are separated by / in the output. An empty string returns an empty string without error.

§Errors

Returns MorseError::UnknownChar for any character that has no Morse representation (e.g. punctuation other than digits and letters).

§Examples

use rune_morse::encode;

assert_eq!(encode("SOS").unwrap(), "... --- ...");
assert_eq!(encode("sos").unwrap(), "... --- ...");
assert_eq!(encode("HI MOM").unwrap(), ".... .. / -- --- --");
assert_eq!(encode("").unwrap(), "");