'How can I get the byte representation of a format!() String in a single expression?

I am trying to format a string and then, in one expression, get a slice of u8s containing the byte representation of the string.

Running

format!("bedrijfpda{:0>3}", num).as_bytes()

gives me a temporary value dropped while borrowed error, but running

let val = format!("bedrijfpda{:0>3}", num);
let bytes = val.as_bytes();

gives me the result I want.

How can I do this in one expression?



Solution 1:[1]

You cannot. If this must be a single expression (maybe your hard drive is full and cannot store the newlines?), convert it to a Vec<u8>:

format!("").into_bytes();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shepmaster