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 Mar 28, 2023
1 parent 21a2ed1 commit 096aeba
Show file tree
Hide file tree
Showing 104 changed files with 139 additions and 139 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!`
// ANCHOR_END: here
println!("{s}"); // This will print `hello, 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;

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 @@ -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:?}!");
}
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
}
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}");
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ fn main() {
r = &x;
}

println!("r: {}", r);
println!("r: {r}");
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ fn main() {
r = &x; // | |
} // -+ |
// |
println!("r: {}", r); // |
println!("r: {r}"); // |
} // ---------+
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ fn main() {
// |
let r = &x; // --+-- 'a |
// | |
println!("r: {}", r); // | |
println!("r: {r}"); // | |
// --+ |
} // ----------+
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ fn main() {
let string2 = "xyz";

let result = longest(string1.as_str(), string2);
println!("The longest string is {}", result);
println!("The longest string is {result}");
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ fn main() {
let string2 = "xyz";

let result = longest(string1.as_str(), string2);
println!("The longest string is {}", result);
println!("The longest string is {result}");
}

// ANCHOR: here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ fn main() {
let string2 = "xyz";

let result = longest(string1.as_str(), string2);
println!("The longest string is {}", result);
println!("The longest string is {result}");
}

// ANCHOR: here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fn main() {
{
let string2 = String::from("xyz");
let result = longest(string1.as_str(), string2.as_str());
println!("The longest string is {}", result);
println!("The longest string is {result}");
}
}
// ANCHOR_END: here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ error[E0597]: `string2` does not live long enough
| ^^^^^^^^^^^^^^^^ borrowed value does not live long enough
7 | }
| - `string2` dropped here while still borrowed
8 | println!("The longest string is {}", result);
| ------ borrow later used here
8 | println!("The longest string is {result}");
| ------ borrow later used here

For more information about this error, try `rustc --explain E0597`.
error: could not compile `chapter10` due to previous error
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fn main() {
let string2 = String::from("xyz");
result = longest(string1.as_str(), string2.as_str());
}
println!("The longest string is {}", result);
println!("The longest string is {result}");
}
// ANCHOR_END: here

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ fn main() {
let string2 = "efghijklmnopqrstuvwxyz";

let result = longest(string1.as_str(), string2);
println!("The longest string is {}", result);
println!("The longest string is {result}");
}

// ANCHOR: here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ fn main() {
let string2 = "xyz";

let result = longest(string1.as_str(), string2);
println!("The longest string is {}", result);
println!("The longest string is {result}");
}

// ANCHOR: here
Expand Down
Loading

0 comments on commit 096aeba

Please sign in to comment.