Skip to main content

Module rust

Module rust 

Source
Expand description

rust literal encoders.

encodes untrusted strings for safe embedding in rust source literals.

§encoding rules

all three encoders use rust’s native escape syntax:

  • named escapes: \0, \t, \n, \r, \\
  • C0 controls and DEL without named escapes → \xHH
  • unicode non-characters → space (string/char) or \xHH per byte (byte string)
  • bidi formatting controls → \u{HHHH} (string/char) or \xHH per byte (byte string)

the encoders differ in which quote is escaped and how non-ASCII is handled:

encoderquote escapenon-ASCII
for_rust_string"\"passes through
for_rust_char'\'passes through
for_rust_byte_string"\"each UTF-8 byte → \xHH

§char literal length

a rust char literal holds exactly one unicode scalar value, so the char encoders require input of exactly one character. empty or longer input encodes to a literal body that does not compile.

Functions§

for_rust_byte_string
encodes input for safe embedding in a rust byte string literal (b"...").
for_rust_char
encodes input for safe embedding in a rust char literal ('...').
for_rust_char_checked
encodes input for a rust char literal ('...'), or returns None if input is not exactly one unicode scalar value.
for_rust_string
encodes input for safe embedding in a rust string literal ("...").
write_rust_byte_string
writes the rust-byte-string-encoded form of input to out.
write_rust_char
writes the rust-char-encoded form of input to out.
write_rust_string
writes the rust-string-encoded form of input to out.