mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-05-16 07:36:05 +02:00
Use dbg! instead of println! in day 3 & 4. (#2669)
Part of #2478 to clean up code blocks when all that is needed is a trivial debug print statement. Co-authored-by: Eric Githinji <egithinji@google.com>
This commit is contained in:
parent
241c28ed7e
commit
ea53e3c935
@ -15,7 +15,7 @@ fn main() {
|
|||||||
let x = 10;
|
let x = 10;
|
||||||
&x
|
&x
|
||||||
};
|
};
|
||||||
println!("x: {x_ref}");
|
dbg!(x_ref);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -37,8 +37,8 @@ fn main() {
|
|||||||
*c = 20;
|
*c = 20;
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("a: {a}");
|
dbg!(a);
|
||||||
println!("b: {b}");
|
dbg!(b);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -57,8 +57,8 @@ fn main() {
|
|||||||
conflict. Replace `c` with a direct mutation of `a` and demonstrate that
|
conflict. Replace `c` with a direct mutation of `a` and demonstrate that
|
||||||
this produces a similar error. This is because direct mutation of a value
|
this produces a similar error. This is because direct mutation of a value
|
||||||
effectively creates a temporary mutable reference.
|
effectively creates a temporary mutable reference.
|
||||||
- Move the `println!` statement for `b` before the scope that introduces `c` to
|
- Move the `dbg!` statement for `b` before the scope that introduces `c` to make
|
||||||
make the code compile.
|
the code compile.
|
||||||
- After that change, the compiler realizes that `b` is only ever used before
|
- After that change, the compiler realizes that `b` is only ever used before
|
||||||
the new mutable borrow of `a` through `c`. This is a feature of the borrow
|
the new mutable borrow of `a` through `c`. This is a feature of the borrow
|
||||||
checker called "non-lexical lifetimes".
|
checker called "non-lexical lifetimes".
|
||||||
|
@ -13,7 +13,7 @@ fn main() {
|
|||||||
let mut vec = vec![1, 2, 3, 4, 5];
|
let mut vec = vec![1, 2, 3, 4, 5];
|
||||||
let elem = &vec[2];
|
let elem = &vec[2];
|
||||||
vec.push(6);
|
vec.push(6);
|
||||||
println!("{elem}");
|
dbg!(elem);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ fn main() {
|
|||||||
let cell = Cell::new(5);
|
let cell = Cell::new(5);
|
||||||
|
|
||||||
cell.set(123);
|
cell.set(123);
|
||||||
println!("{}", cell.get());
|
dbg!(cell.get());
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ Rust will trigger a panic if a fatal error happens at runtime:
|
|||||||
```rust,editable,should_panic
|
```rust,editable,should_panic
|
||||||
fn main() {
|
fn main() {
|
||||||
let v = vec![10, 20, 30];
|
let v = vec![10, 20, 30];
|
||||||
println!("v[100]: {}", v[100]);
|
dbg!(v[100]);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -33,12 +33,12 @@ use std::panic;
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let result = panic::catch_unwind(|| "No problem here!");
|
let result = panic::catch_unwind(|| "No problem here!");
|
||||||
println!("{result:?}");
|
dbg!(result);
|
||||||
|
|
||||||
let result = panic::catch_unwind(|| {
|
let result = panic::catch_unwind(|| {
|
||||||
panic!("oh no!");
|
panic!("oh no!");
|
||||||
});
|
});
|
||||||
println!("{result:?}");
|
dbg!(result);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -32,7 +32,7 @@ fn main() {
|
|||||||
let slice = &[2, 4, 6, 8];
|
let slice = &[2, 4, 6, 8];
|
||||||
let iter = SliceIter { slice, i: 0 };
|
let iter = SliceIter { slice, i: 0 };
|
||||||
for elem in iter {
|
for elem in iter {
|
||||||
println!("elem: {elem}");
|
dbg!(elem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -40,7 +40,7 @@ fn main() {
|
|||||||
let p1: Point = Point(10, 10);
|
let p1: Point = Point(10, 10);
|
||||||
let p2: Point = Point(20, 20);
|
let p2: Point = Point(20, 20);
|
||||||
let p3 = left_most(&p1, &p2); // What is the lifetime of p3?
|
let p3 = left_most(&p1, &p2); // What is the lifetime of p3?
|
||||||
println!("p3: {p3:?}");
|
dbg!(p3);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -24,8 +24,8 @@ fn main() {
|
|||||||
let noun = Highlight { slice: &doc[16..19], color: HighlightColor::Yellow };
|
let noun = Highlight { slice: &doc[16..19], color: HighlightColor::Yellow };
|
||||||
let verb = Highlight { slice: &doc[20..25], color: HighlightColor::Pink };
|
let verb = Highlight { slice: &doc[20..25], color: HighlightColor::Pink };
|
||||||
// drop(doc);
|
// drop(doc);
|
||||||
println!("{noun:?}");
|
dbg!(noun);
|
||||||
println!("{verb:?}");
|
dbg!(verb);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -12,8 +12,8 @@ While move semantics are the default, certain types are copied by default:
|
|||||||
fn main() {
|
fn main() {
|
||||||
let x = 42;
|
let x = 42;
|
||||||
let y = x;
|
let y = x;
|
||||||
println!("x: {x}"); // would not be accessible if not Copy
|
dbg!(x); // would not be accessible if not Copy
|
||||||
println!("y: {y}");
|
dbg!(y);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -113,16 +113,16 @@ impl PackageBuilder {
|
|||||||
// ANCHOR: main
|
// ANCHOR: main
|
||||||
fn main() {
|
fn main() {
|
||||||
let base64 = PackageBuilder::new("base64").version("0.13").build();
|
let base64 = PackageBuilder::new("base64").version("0.13").build();
|
||||||
println!("base64: {base64:?}");
|
dbg!(&base64);
|
||||||
let log =
|
let log =
|
||||||
PackageBuilder::new("log").version("0.4").language(Language::Rust).build();
|
PackageBuilder::new("log").version("0.4").language(Language::Rust).build();
|
||||||
println!("log: {log:?}");
|
dbg!(&log);
|
||||||
let serde = PackageBuilder::new("serde")
|
let serde = PackageBuilder::new("serde")
|
||||||
.authors(vec!["djmitche".into()])
|
.authors(vec!["djmitche".into()])
|
||||||
.version(String::from("4.0"))
|
.version(String::from("4.0"))
|
||||||
.dependency(base64.as_dependency())
|
.dependency(base64.as_dependency())
|
||||||
.dependency(log.as_dependency())
|
.dependency(log.as_dependency())
|
||||||
.build();
|
.build();
|
||||||
println!("serde: {serde:?}");
|
dbg!(serde);
|
||||||
}
|
}
|
||||||
// ANCHOR_END: main
|
// ANCHOR_END: main
|
||||||
|
@ -10,8 +10,8 @@ An assignment will transfer _ownership_ between variables:
|
|||||||
fn main() {
|
fn main() {
|
||||||
let s1: String = String::from("Hello!");
|
let s1: String = String::from("Hello!");
|
||||||
let s2: String = s1;
|
let s2: String = s1;
|
||||||
println!("s2: {s2}");
|
dbg!(s2);
|
||||||
// println!("s1: {s1}");
|
// dbg!(s1);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -15,9 +15,9 @@ struct Point(i32, i32);
|
|||||||
fn main() {
|
fn main() {
|
||||||
{
|
{
|
||||||
let p = Point(3, 4);
|
let p = Point(3, 4);
|
||||||
println!("x: {}", p.0);
|
dbg!(p.0);
|
||||||
}
|
}
|
||||||
println!("y: {}", p.1);
|
dbg!(p.1);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -14,8 +14,8 @@ fn main() {
|
|||||||
let a = Rc::new(10);
|
let a = Rc::new(10);
|
||||||
let b = Rc::clone(&a);
|
let b = Rc::clone(&a);
|
||||||
|
|
||||||
println!("a: {a}");
|
dbg!(a);
|
||||||
println!("b: {b}");
|
dbg!(b);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user