9951 explained code solutions for 126 technologies


rustWhat are the escape sequences for Rust strings?


Rust strings support a variety of escape sequences to represent special characters. The following are the escape sequences for Rust strings:

  1. \n: This is used to represent a new line.

Example code

let s = "Hello\nWorld";
println!("{}", s);

Output example

Hello
World
  1. \t: This is used to represent a tab character.

Example code

let s = "Hello\tWorld";
println!("{}", s);

Output example

Hello   World
  1. \r: This is used to represent a carriage return.

Example code

let s = "Hello\rWorld";
println!("{}", s);

Output example

World
  1. \0: This is used to represent the null character.

Example code

let s = "Hello\0World";
println!("{}", s);

Output example

Hello
  1. \\: This is used to represent a backslash character.

Example code

let s = "Hello\\World";
println!("{}", s);

Output example

Hello\World

Helpful links

Edit this code on GitHub