I do not use any crates having dependencies. So recently I tried the color crate, having just one optional dependency. It has some strange things, but I became curious about the following code:
let binding = "strikethrough".strikethrough();
let color = binding.green();
println!("{} and {}", color, "italic".italic().red());
Why I can chain applying color attributes to a string as a part of println, but if I want to do the same out of println, I have to apply attributes separately?
Another question isn't related to the crate, but also very weird. Why do I get the warning?
warning: unused variable: `err`
--> docgi.rs:1203:13
|
1203 | Err(err) => { // probably symlink, skip
| ^^^ help: if this is intentional, prefix it with an underscore: `_err`
|
= note: `#[warn(unused_variables)]` on by default
warning: 1 warning emitted
I actually use the err in the code,
let meta = match path.metadata() {
Ok(metadata) => metadata,
Err(err) => { // probably symlink, skip
eprintln!("No metadata for {path:?} {err:?}");
buf.push_str("\", \"type\": \"dead\"}");
return Ok(buf)},
};
I tried to build rustc, and also tried rustc from rustup on other machine. The error is consistent:
error[E0716]: temporary value dropped while borrowed
--> test.rs:47:13
|
47 | let s = "hello, world".strikethrough().green();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
| |
| creates a temporary value which is freed while still in use
48 | println!("{s}");
| --- borrow later used here
|
help: consider using a `let` binding to create a longer lived value
|
47 ~ let binding = "hello, world".strikethrough();
48 ~ let s = binding.green();
Regarding other errors, yes, it happens on Raspberry Pi only, so I guess I need to upgrade the compiler.
What version of colored crate are you using? In the current version, there should not be anything like this, since there are no temporaries - every conversion is "from owned to owned", the only borrowed value is the initial &str.
Aha, so it's not colored, it's owo-colors. They decided to do this in another way, namely, to make "colored" values borrow from the original, not own it. So inside println you can do anything in one go (since it will release the temporary once the line ends), but if you store the result somewhere, you have to store the original value, too.
Indeed, it was my overlook. When I build a release, all eprintln get gone, and the compiler warns me. Perhaps, I need also to add a condition for exposing the variable.