Skip to content

Commit

Permalink
Use v1.58 captured ident formatting in examples
Browse files Browse the repository at this point in the history
Per #3047, use captured identifiers instead of the positional ones for some examples, e.g.

```diff
-  println!("Worker {} got a job; executing.", id);
+  println!("Worker {id} got a job; executing.");
```
  • Loading branch information
nyurik committed Feb 6, 2022
1 parent c8a9ac9 commit 1197d3b
Show file tree
Hide file tree
Showing 38 changed files with 53 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn main() {
// ANCHOR_END: expect

// ANCHOR: print_guess
println!("You guessed: {}", guess);
println!("You guessed: {guess}");
// ANCHOR_END: print_guess
}
// ANCHOR: all
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ fn main() {
.read_line(&mut guess)
.expect("Failed to read line");

println!("You guessed: {}", guess);
println!("You guessed: {guess}");
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ fn main() {
let mut number = 3;

while number != 0 {
println!("{}!", number);
println!("{number}!");

number -= 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ fn main() {
}

fn print_labeled_measurement(value: i32, unit_label: char) {
println!("The measurement is: {}{}", value, unit_label);
println!("The measurement is: {value}{unit_label}");
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fn main() {
for number in (1..4).rev() {
println!("{}!", number);
println!("{number}!");
}
println!("LIFTOFF!!!");
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ fn main() {

let (s2, len) = calculate_length(s1);

println!("The length of '{}' is {}.", s2, len);
println!("The length of '{s2}' is {len}.");
}

fn calculate_length(s: String) -> (String, usize) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ fn main() {
let s1 = String::from("hello");
let s2 = s1.clone();

println!("s1 = {}, s2 = {}", s1, s2);
println!("s1 = {s1}, s2 = {s2}");
// ANCHOR_END: here
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ fn main() {
let x = 5;
let y = x;

println!("x = {}, y = {}", x, y);
println!("x = {x}, y = {y}");
// ANCHOR_END: here
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fn main() {
let len = calculate_length(&s1);
// ANCHOR_END: here

println!("The length of '{}' is {}.", s1, len);
println!("The length of '{s1}' is {len}.");
}

fn calculate_length(s: &String) -> usize {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ fn main() {

let len = calculate_length(&s1);

println!("The length of '{}' is {}.", s1, len);
println!("The length of '{s1}' is {len}.");
}

// ANCHOR: here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn value_in_cents(coin: Coin) -> u8 {
Coin::Nickel => 5,
Coin::Dime => 10,
Coin::Quarter(state) => {
println!("State quarter from {:?}!", state);
println!("State quarter from {state:?}!");
25
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() {
// ANCHOR: here
let mut count = 0;
match coin {
Coin::Quarter(state) => println!("State quarter from {:?}!", state),
Coin::Quarter(state) => println!("State quarter from {state:?}!"),
_ => count += 1,
}
// ANCHOR_END: here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() {
// ANCHOR: here
let mut count = 0;
if let Coin::Quarter(state) = coin {
println!("State quarter from {:?}!", state);
println!("State quarter from {state:?}!");
} else {
count += 1;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub fn greeting(name: &str) -> String {
format!("Hello {}!", name)
format!("Hello {name}!")
}

#[cfg(test)]
Expand Down
6 changes: 3 additions & 3 deletions listings/ch13-functional-features/listing-13-04/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ fn generate_workout(intensity: u32, random_number: u32) {
let expensive_result = simulated_expensive_calculation(intensity);

if intensity < 25 {
println!("Today, do {} pushups!", expensive_result);
println!("Next, do {} situps!", expensive_result);
println!("Today, do {expensive_result} pushups!");
println!("Next, do {expensive_result} situps!");
} else {
if random_number == 3 {
println!("Take a break today! Remember to stay hydrated!");
} else {
println!("Today, run for {} minutes!", expensive_result);
println!("Today, run for {expensive_result} minutes!");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion listings/ch15-smart-pointers/listing-15-11/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ANCHOR: here
fn hello(name: &str) {
println!("Hello, {}!", name);
println!("Hello, {name}!");
}
// ANCHOR_END: here

Expand Down
2 changes: 1 addition & 1 deletion listings/ch15-smart-pointers/listing-15-12/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl<T> MyBox<T> {
}

fn hello(name: &str) {
println!("Hello, {}!", name);
println!("Hello, {name}!");
}

// ANCHOR: here
Expand Down
2 changes: 1 addition & 1 deletion listings/ch15-smart-pointers/listing-15-13/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl<T> MyBox<T> {
}

fn hello(name: &str) {
println!("Hello, {}!", name);
println!("Hello, {name}!");
}

// ANCHOR: here
Expand Down
4 changes: 2 additions & 2 deletions listings/ch16-fearless-concurrency/listing-16-01/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use std::time::Duration;
fn main() {
thread::spawn(|| {
for i in 1..10 {
println!("hi number {} from the spawned thread!", i);
println!("hi number {i} from the spawned thread!");
thread::sleep(Duration::from_millis(1));
}
});

for i in 1..5 {
println!("hi number {} from the main thread!", i);
println!("hi number {i} from the main thread!");
thread::sleep(Duration::from_millis(1));
}
}
4 changes: 2 additions & 2 deletions listings/ch16-fearless-concurrency/listing-16-02/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use std::time::Duration;
fn main() {
let handle = thread::spawn(|| {
for i in 1..10 {
println!("hi number {} from the spawned thread!", i);
println!("hi number {i} from the spawned thread!");
thread::sleep(Duration::from_millis(1));
}
});

for i in 1..5 {
println!("hi number {} from the main thread!", i);
println!("hi number {i} from the main thread!");
thread::sleep(Duration::from_millis(1));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ use std::time::Duration;
fn main() {
let handle = thread::spawn(|| {
for i in 1..10 {
println!("hi number {} from the spawned thread!", i);
println!("hi number {i} from the spawned thread!");
thread::sleep(Duration::from_millis(1));
}
});

handle.join().unwrap();

for i in 1..5 {
println!("hi number {} from the main thread!", i);
println!("hi number {i} from the main thread!");
thread::sleep(Duration::from_millis(1));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ fn main() {
let age: Result<u8, _> = "34".parse();

if let Some(color) = favorite_color {
println!("Using your favorite color, {}, as the background", color);
println!("Using your favorite color, {color}, as the background");
} else if is_tuesday {
println!("Tuesday is green day!");
} else if let Ok(age) = age {
Expand Down
6 changes: 3 additions & 3 deletions listings/ch18-patterns-and-matching/listing-18-11/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ fn main() {

match x {
Some(50) => println!("Got 50"),
Some(y) => println!("Matched, y = {:?}", y),
_ => println!("Default case, x = {:?}", x),
Some(y) => println!("Matched, y = {y:?}"),
_ => println!("Default case, x = {x:?}"),
}

println!("at the end: x = {:?}, y = {:?}", x, y);
println!("at the end: x = {x:?}, y = {y:?}");
// ANCHOR_END: here
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ fn main() {

match numbers {
(first, _, third, _, fifth) => {
println!("Some numbers: {}, {}, {}", first, third, fifth)
println!("Some numbers: {first}, {third}, {fifth}")
}
}
// ANCHOR_END: here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ fn main() {

match numbers {
(first, .., last) => {
println!("Some numbers: {}, {}", first, last);
println!("Some numbers: {first}, {last}");
}
}
}
6 changes: 3 additions & 3 deletions listings/ch18-patterns-and-matching/listing-18-27/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ fn main() {

match x {
Some(50) => println!("Got 50"),
Some(n) if n == y => println!("Matched, n = {}", n),
_ => println!("Default case, x = {:?}", x),
Some(n) if n == y => println!("Matched, n = {n}"),
_ => println!("Default case, x = {x:?}"),
}

println!("at the end: x = {:?}, y = {}", x, y);
println!("at the end: x = {x:?}, y = {y}");
}
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-20/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl Worker {
let thread = thread::spawn(move || loop {
let job = receiver.lock().unwrap().recv().unwrap();

println!("Worker {} got a job; executing.", id);
println!("Worker {id} got a job; executing.");

job();
});
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-21/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Worker {
fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Job>>>) -> Worker {
let thread = thread::spawn(move || {
while let Ok(job) = receiver.lock().unwrap().recv() {
println!("Worker {} got a job; executing.", id);
println!("Worker {id} got a job; executing.");

job();
}
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-22/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl Worker {
let thread = thread::spawn(move || loop {
let job = receiver.lock().unwrap().recv().unwrap();

println!("Worker {} got a job; executing.", id);
println!("Worker {id} got a job; executing.");

job();
});
Expand Down
4 changes: 2 additions & 2 deletions listings/ch20-web-server/listing-20-23/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ impl Worker {

match message {
Message::NewJob(job) => {
println!("Worker {} got a job; executing.", id);
println!("Worker {id} got a job; executing.");

job();
}
Message::Terminate => {
println!("Worker {} was told to terminate.", id);
println!("Worker {id} was told to terminate.");

break;
}
Expand Down
4 changes: 2 additions & 2 deletions listings/ch20-web-server/listing-20-24/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ impl Worker {

match message {
Message::NewJob(job) => {
println!("Worker {} got a job; executing.", id);
println!("Worker {id} got a job; executing.");

job();
}
Message::Terminate => {
println!("Worker {} was told to terminate.", id);
println!("Worker {id} was told to terminate.");

break;
}
Expand Down
4 changes: 2 additions & 2 deletions listings/ch20-web-server/listing-20-25/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ impl Worker {

match message {
Message::NewJob(job) => {
println!("Worker {} got a job; executing.", id);
println!("Worker {id} got a job; executing.");

job();
}
Message::Terminate => {
println!("Worker {} was told to terminate.", id);
println!("Worker {id} was told to terminate.");

break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl Worker {
let thread = thread::spawn(move || loop {
let job = receiver.lock().unwrap().recv().unwrap();

println!("Worker {} got a job; executing.", id);
println!("Worker {id} got a job; executing.");

job();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl Worker {
let thread = thread::spawn(move || loop {
let job = receiver.lock().unwrap().recv().unwrap();

println!("Worker {} got a job; executing.", id);
println!("Worker {id} got a job; executing.");

job();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl Worker {
let thread = thread::spawn(move || loop {
let job = receiver.lock().unwrap().recv().unwrap();

println!("Worker {} got a job; executing.", id);
println!("Worker {id} got a job; executing.");

job();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl Worker {
let thread = thread::spawn(move || loop {
let job = receiver.lock().unwrap().recv().unwrap();

println!("Worker {} got a job; executing.", id);
println!("Worker {id} got a job; executing.");

job();
});
Expand Down
4 changes: 2 additions & 2 deletions listings/ch20-web-server/no-listing-08-final-code/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ impl Worker {

match message {
Message::NewJob(job) => {
println!("Worker {} got a job; executing.", id);
println!("Worker {id} got a job; executing.");

job();
}
Message::Terminate => {
println!("Worker {} was told to terminate.", id);
println!("Worker {id} was told to terminate.");

break;
}
Expand Down
Loading

0 comments on commit 1197d3b

Please sign in to comment.