[Avg. reading time: 4 minutes]
Escape Printing
Rust supports standard escape sequences similar to other languages. Similar to other programming languages \t and \n are used for Tab and Newlines
Common Escape Characters
- \n → New line
- \t → Tab
- \ → Backslash
- " → Double quote
// \t \n fn main() { print!("\t first line is tabbed \nand second line is on a new line"); }
Printing Escape Characters Literally
// Escape Characters fn main() { println!("Here are two escape characters: \\n and \\t"); }
Print multiple escape characters
// Print multiple \\ , " fn main() { println!("File \"folder location is at c:\\users\\ganesh\\Documents\\01.rs.\" ") }
Too many \ there is a good chance one might forget to \, is there an easy way?
// r# and # fn main() { println!(r#"File "folder location is at c:\users\ganesh\Documents\01.rs." "#) }
If you need to print #, then use ##
Raw Identifiers (r#)
// # & ## fn main() { let hashtag_string = r##"The hashtag #niceToSeeYou had become very popular."##; // Has one # so we need at least ## let many_hashtags = r####""You don't have to type ### to use a hashtag. You can just use #.""####; // Has three ### so we need at least #### println!("{}\n{}\n", hashtag_string, many_hashtags); }
Alternate Use
Not a good programming practice to use keywords as variables
// Using reserved words fn main() { let let=4; println!("{}", let); }
// r# fn main() { let r#let=4; println!("{}", r#let); }