<buttonid="sidebar-toggle"class="icon-button"type="button"title="Toggle Table of Contents"aria-label="Toggle Table of Contents"aria-controls="sidebar">
<ahref="print.html"title="Print this book"aria-label="Print this book">
<iid="print-button"class="fa fa-print"></i>
</a>
</div>
</div>
<divid="search-wrapper"class="hidden">
<formid="searchbar-outer"class="searchbar-outer">
<inputtype="search"name="search"id="searchbar"name="searchbar"placeholder="Search this book ..."aria-controls="searchresults-outer"aria-describedby="searchresults-header">
println!("Here are two escape characters: \\n and \\t");
}
</code></pre></pre>
<p>This prints:</p>
<pre><codeclass="language-text">Here are two escape characters: \n and \t
</code></pre>
<p>Sometimes you have too many <code>"</code> and escape characters, and want Rust to ignore everything. To do this, you can add <code>r#</code> to the beginning and <code>#</code> to the end.</p>
println!("He said, \"You can find the file at c:\\files\\my_documents\\file.txt.\" Then I found the file."); // We used \ five times here
println!(r#"He said, "You can find the file at c:\files\my_documents\file.txt." Then I found the file."#)
}
</code></pre></pre>
<p>This prints the same thing, but using <code>r#</code> makes it easier for humans to read.</p>
<pre><codeclass="language-text">He said, "You can find the file at c:\files\my_documents\file.txt." Then I found the file.
He said, "You can find the file at c:\files\my_documents\file.txt." Then I found the file.
</code></pre>
<p>If you need to print with a <code>#</code> inside, then you can start with <code>r##</code> and end with <code>##</code>. And if you need more than one, you can add one more # on each side.</p>
let my_string = "'Ice to see you,' he said."; // single quotes
let quote_string = r#""Ice to see you," he said."#; // double quotes
let hashtag_string = r##"The hashtag #IceToSeeYou 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 ####
let mut r#mut = 10; // This variable's name is mut
}
</code></pre></pre>
<p><code>r#</code> has this function because older versions of Rust had fewer keywords than Rust now. So with <code>r#</code> you can avoid mistakes with variable names that were not keywords before.</p>
<p>Or maybe for some reason you <em>really</em> need a function to have a name like <code>return</code>. Then you can write this:</p>
<pre><codeclass="language-text">Here is your number.
8
</code></pre>
<p>So you probably won't need it, but if you really need to use a keyword for a variable then you can use <code>r#</code>.</p>
<p>If you want to print the bytes of a <code>&str</code> or a <code>char</code>, you can just write <code>b</code> before the string. This works for all ASCII characters. These are all the ASCII characters:</p>
<p>There is also a Unicode escape that lets you print any Unicode character inside a string: <code>\u{}</code>. A hexadecimal number goes inside the <code>{}</code> to print it. Here is a short example of how to get the Unicode number, and how to print it again.</p>
println!("{:X}", '행' as u32); // Cast char as u32 to get the hexadecimal value
println!("{:X}", 'H' as u32);
println!("{:X}", '居' as u32);
println!("{:X}", 'い' as u32);
println!("\u{D589}, \u{48}, \u{5C45}, \u{3044}"); // Try printing them with unicode escape \u
}
</code></pre></pre>
<p>We know that <code>println!</code> can print with <code>{}</code> (for Display) and <code>{:?}</code> (for Debug), plus <code>{:#?}</code> for pretty printing. But there are many other ways to print.</p>
<p>For example, if you have a reference, you can use <code>{:p}</code> to print the <em>pointer address</em>. Pointer address means the location in your computer's memory.</p>
println!("This is {1} {2}, son of {0} {2}.", father_name, son_name, family_name);
}
</code></pre></pre>
<p><code>father_name</code> is in position 0, <code>son_name</code> is in position 1, and <code>family_name</code> is in position 2. So it prints <code>This is Adrian Fahrenheit Țepeș, son of Vlad Țepeș</code>.</p>
<p>Maybe you have a very complex string to print with too many variables inside the <code>{}</code> curly brackets. Or maybe you need to print a variable more than one time. Then it can help to add names to the <code>{}</code>:</p>
<p>This prints <code>ㅎㅎㅎㅎㅎaㅎㅎㅎㅎㅎ</code>. Let's look at 1) to 5) for this to understand how the compiler reads it.</p>
<ul>
<li>Do you want a variable name? <code>{:ㅎ^11}</code> There is no variable name. There is nothing before <code>:</code>.</li>
<li>Do you want a padding character? <code>{:ㅎ^11}</code> Yes. ㅎ comes after the <code>:</code> and has a <code>^</code>. <code><</code> means padding with the character on the left, <code>></code> means on the right, and <code>^</code> means in the middle.</li>
<li>Do you want a minimum length? <code>{:ㅎ^11}</code> Yes: there is an 11 after.</li>
<li>Do you want a maximum length? <code>{:ㅎ^11}</code> No: there is no number with a <code>.</code> before.</li>
</ul>
<p>Here is an example of many types of formatting.</p>
println!("{:-^30}", title); // no variable name, pad with -, put in centre, 30 characters long
let bar = "|";
println!("{: <15}{: >15}", bar, bar); // no variable name, pad with space, 15 characters each, one to the left, one to the right
let a = "SEOUL";
let b = "TOKYO";
println!("{city1:-<15}{city2:->15}", city1 = a, city2 = b); // variable names city1 and city2, pad with -, one to the left, one to the right