Skip to content

Commit

Permalink
Inline all format arguments
Browse files Browse the repository at this point in the history
The inlined format arguments are a bit easier to understand,
especially for those familiar with the other languages (python, js).
  • Loading branch information
nyurik committed Oct 13, 2022
1 parent f1e5ad8 commit a5eb0a4
Show file tree
Hide file tree
Showing 115 changed files with 164 additions and 164 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "rust-book"
version = "0.0.1"
description = "The Rust Book"
edition = "2018"
edition = "2021"

[[bin]]
name = "concat_chapters"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ fn main() {
// do stuff with s
} // this scope is now over, and s is no longer valid
// ANCHOR_END: here
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ fn main() {
// special happens.

fn takes_ownership(some_string: String) { // some_string comes into scope
println!("{}", some_string);
println!("{some_string}");
} // Here, some_string goes out of scope and `drop` is called. The backing
// memory is freed.

fn makes_copy(some_integer: i32) { // some_integer comes into scope
println!("{}", some_integer);
println!("{some_integer}");
} // Here, some_integer goes out of scope. Nothing special happens.
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 @@ -4,6 +4,6 @@ fn main() {

s.push_str(", world!"); // push_str() appends a literal to a String

println!("{}", s); // This will print `hello, world!`
println!("{s}"); // This will print `hello, world!`
// ANCHOR_END: here
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ error[E0382]: borrow of moved value: `s1`
| -- move occurs because `s1` has type `String`, which does not implement the `Copy` trait
3 | let s2 = s1;
| -- value moved here
4 |
5 | println!("{}, world!", s1);
| ^^ value borrowed here after move
4 |
5 | println!("{s1}, world!");
| ^^ value borrowed here after move
|
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)

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;

println!("{}, world!", s1);
println!("{s1}, world!");
// ANCHOR_END: here
}
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 @@ -4,10 +4,10 @@ fn main() {

let r1 = &s; // no problem
let r2 = &s; // no problem
println!("{} and {}", r1, r2);
println!("{r1} and {r2}");
// variables r1 and r2 will not be used after this point

let r3 = &mut s; // no problem
println!("{}", r3);
println!("{r3}");
// ANCHOR_END: here
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ error[E0502]: cannot borrow `s` as mutable because it is also borrowed as immuta
|
16 | let word = first_word(&s);
| -- immutable borrow occurs here
17 |
17 |
18 | s.clear(); // error!
| ^^^^^^^^^ mutable borrow occurs here
19 |
20 | println!("the first word is: {}", word);
| ---- immutable borrow later used here
19 |
20 | println!("the first word is: {word}");
| ---- immutable borrow later used here

For more information about this error, try `rustc --explain E0502`.
error: could not compile `ownership` due to previous error
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ fn main() {

s.clear(); // error!

println!("the first word is: {}", word);
println!("the first word is: {word}");
}
// ANCHOR_END: here
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ fn main() {
height: 50,
};

println!("rect1 is {:?}", rect1);
println!("rect1 is {rect1:?}");
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ fn main() {
height: 50,
};

println!("rect1 is {:#?}", rect1);
println!("rect1 is {rect1:#?}");
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ fn main() {
// ANCHOR: here
let config_max = Some(3u8);
match config_max {
Some(max) => println!("The maximum is configured to be {}", max),
Some(max) => println!("The maximum is configured to be {max}"),
_ => (),
}
// ANCHOR_END: 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 @@ -2,7 +2,7 @@ fn main() {
// ANCHOR: here
let config_max = Some(3u8);
if let Some(max) = config_max {
println!("The maximum is configured to be {}", max);
println!("The maximum is configured to be {max}");
}
// ANCHOR_END: here
}
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
Expand Up @@ -4,5 +4,5 @@ pub mod garden;

fn main() {
let plant = Asparagus {};
println!("I'm growing {:?}!", plant);
println!("I'm growing {plant:?}!");
}
4 changes: 2 additions & 2 deletions listings/ch08-common-collections/listing-08-04/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ fn main() {
let v = vec![1, 2, 3, 4, 5];

let third: &i32 = &v[2];
println!("The third element is {}", third);
println!("The third element is {third}");

let third: Option<&i32> = v.get(2);
match third {
Some(third) => println!("The third element is {}", third),
Some(third) => println!("The third element is {third}"),
None => println!("There is no third element."),
}
// ANCHOR_END: here
Expand Down
8 changes: 4 additions & 4 deletions listings/ch08-common-collections/listing-08-06/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immuta
|
4 | let first = &v[0];
| - immutable borrow occurs here
5 |
5 |
6 | v.push(6);
| ^^^^^^^^^ mutable borrow occurs here
7 |
8 | println!("The first element is: {}", first);
| ----- immutable borrow later used here
7 |
8 | println!("The first element is: {first}");
| ----- immutable borrow later used here

For more information about this error, try `rustc --explain E0502`.
error: could not compile `collections` due to previous error
2 changes: 1 addition & 1 deletion listings/ch08-common-collections/listing-08-06/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ fn main() {

v.push(6);

println!("The first element is: {}", first);
println!("The first element is: {first}");
// ANCHOR_END: here
}
2 changes: 1 addition & 1 deletion listings/ch08-common-collections/listing-08-07/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ fn main() {
// ANCHOR: here
let v = vec![100, 32, 57];
for i in &v {
println!("{}", i);
println!("{i}");
}
// ANCHOR_END: here
}
2 changes: 1 addition & 1 deletion listings/ch08-common-collections/listing-08-16/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ fn main() {
let mut s1 = String::from("foo");
let s2 = "bar";
s1.push_str(s2);
println!("s2 is {}", s2);
println!("s2 is {s2}");
// ANCHOR_END: here
}
2 changes: 1 addition & 1 deletion listings/ch08-common-collections/listing-08-23/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ fn main() {
scores.insert(String::from("Blue"), 10);
scores.insert(String::from("Blue"), 25);

println!("{:?}", scores);
println!("{scores:?}");
// ANCHOR_END: here
}
2 changes: 1 addition & 1 deletion listings/ch08-common-collections/listing-08-24/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ fn main() {
scores.entry(String::from("Yellow")).or_insert(50);
scores.entry(String::from("Blue")).or_insert(50);

println!("{:?}", scores);
println!("{scores:?}");
// ANCHOR_END: here
}
2 changes: 1 addition & 1 deletion listings/ch08-common-collections/listing-08-25/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ fn main() {
*count += 1;
}

println!("{:?}", map);
println!("{map:?}");
// ANCHOR_END: here
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn main() {
scores.insert(String::from("Yellow"), 50);

for (key, value) in &scores {
println!("{}: {}", key, value);
println!("{key}: {value}");
}
// ANCHOR_END: here
}
2 changes: 1 addition & 1 deletion listings/ch09-error-handling/listing-09-04/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ fn main() {

let greeting_file = match greeting_file_result {
Ok(file) => file,
Err(error) => panic!("Problem opening the file: {:?}", error),
Err(error) => panic!("Problem opening the file: {error:?}"),
};
}
4 changes: 2 additions & 2 deletions listings/ch09-error-handling/listing-09-05/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ fn main() {
Err(error) => match error.kind() {
ErrorKind::NotFound => match File::create("hello.txt") {
Ok(fc) => fc,
Err(e) => panic!("Problem creating the file: {:?}", e),
Err(e) => panic!("Problem creating the file: {e:?}"),
},
other_error => {
panic!("Problem opening the file: {:?}", other_error);
panic!("Problem opening the file: {other_error:?}");
}
},
};
Expand Down
2 changes: 1 addition & 1 deletion listings/ch09-error-handling/listing-09-13/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct Guess {
impl Guess {
pub fn new(value: i32) -> Guess {
if value < 1 || value > 100 {
panic!("Guess value must be between 1 and 100, got {}.", value);
panic!("Guess value must be between 1 and 100, got {value}.");
}

Guess { value }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn main() {
}
}

println!("The largest number is {}", largest);
println!("The largest number is {largest}");
// ANCHOR_END: here
assert_eq!(*largest, 100);
// ANCHOR: here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn main() {
}
}

println!("The largest number is {}", largest);
println!("The largest number is {largest}");

let number_list = vec![102, 34, 6000, 89, 54, 2, 43, 8];

Expand All @@ -21,5 +21,5 @@ fn main() {
}
}

println!("The largest number is {}", largest);
println!("The largest number is {largest}");
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ fn main() {
let number_list = vec![34, 50, 25, 100, 65];

let result = largest(&number_list);
println!("The largest number is {}", result);
println!("The largest number is {result}");
// ANCHOR_END: here
assert_eq!(*result, 100);
// ANCHOR: here

let number_list = vec![102, 34, 6000, 89, 54, 2, 43, 8];

let result = largest(&number_list);
println!("The largest number is {}", result);
println!("The largest number is {result}");
// ANCHOR_END: here
assert_eq!(*result, 6000);
// ANCHOR: here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ fn main() {
let number_list = vec![34, 50, 25, 100, 65];

let result = largest_i32(&number_list);
println!("The largest number is {}", result);
println!("The largest number is {result}");
// ANCHOR_END: here
assert_eq!(*result, 100);
// ANCHOR: here

let char_list = vec!['y', 'm', 'a', 'q'];

let result = largest_char(&char_list);
println!("The largest char is {}", result);
println!("The largest char is {result}");
// ANCHOR_END: here
assert_eq!(*result, 'y');
// ANCHOR: here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ fn main() {
let number_list = vec![34, 50, 25, 100, 65];

let result = largest(&number_list);
println!("The largest number is {}", result);
println!("The largest number is {result}");

let char_list = vec!['y', 'm', 'a', 'q'];

let result = largest(&char_list);
println!("The largest char is {}", result);
println!("The largest char is {result}");
}
Loading

0 comments on commit a5eb0a4

Please sign in to comment.