Few curiosity questions

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 guess you meant colored - Rust, not color - Rust.

You don't have to? let color = "strikethrough".strikethrough().green() works without any problems, at least by itself.

Also doesn't seem to be the case.

1 Like

I think you mean the colored crate? the color crate is for color space conversions.

I don't know what you mean, but it just works as expected:

let s = "hello, world".strikethrough().green();
println!("{s}");

this shouldn't give a warning, what version of the compiler are you using?

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.

Sorry, since I build the crate myself, sometimes I give it an inconsistent name to the commonly used.

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.

I took it from GitHub, so I do not know the version. Maybe, it's an old repository?

I guess you are right, the doc is from version 3, but the repository is version 4.

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.

Awesome, thanks the clarification.

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.