As mentioned in the comments, anything which has a Display instance automatically gets a ToString instance, so you can call to_string to get a string and then truncate on any value which implements Display. That's probably the practical, correct answer that you're looking for.
But for completeness, there's still an interesting question here: can we do the truncation without ever generating the intermediate string? As it happens, we can implement a custom formatter to do just that. Most Display implementations are more or less calls to the write! macro, which is just a fancy way of calling write_fmt. Confusingly, this can be write_fmt from either the std::io::Write or the std::fmt::Write trait, but we'll focus on the latter for the moment. We can implement our own instance of std::fmt::Write that truncates to a specified length.
pub struct TruncatedFormatter<'a, T> {
pub remaining: usize,
pub inner: &'a mut T,
}
impl<'a, T> fmt::Write for TruncatedFormatter<'a, T> where T : fmt::Write {
fn write_str(&mut self, s: &str) -> fmt::Result {
if self.remaining < s.len() {
self.inner.write_str(&s[0..self.remaining])?;
self.remaining = 0;
Ok(())
} else {
self.remaining -= s.len();
self.inner.write_str(s)
}
}
}
Then we can make a thin wrapper around any type T which forces the type to use our customized formatter. Since fmt on Display only requires an immutable reference, our wrapper will only take an immutable reference, to maximize compatibility with the call site.
pub struct TruncatedValue<'a, T>(pub usize, pub &'a T);
impl<'a, T> fmt::Display for TruncatedValue<'a, T> where T : fmt::Display {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let TruncatedValue(remaining, value) = self;
let mut wrapped_fmt = TruncatedFormatter { remaining: *remaining, inner: f };
write!(wrapped_fmt, "{}", value)
}
}
Then, when we want to print our value truncated to a specific length, we can wrap it during the format! or println! call.
println!("Full value '{}'\nTruncated value '{}'\n", test, TruncatedValue(7, &test));
Try it in the Rust Playground!