Code:
fltk::frame::Frame::new(0,0, 300, 100, format!("side item {}", i));
Output Error:
the trait `std::convert::From<std::string::String>` is not implemented for `std::option::Option<&'static str>`
format!()
yields a String
, while Frame::new expects an (optional) &'static str
, i.e. a string slice that will be alive for the entirety of the program's lifecycle.
This pretty much means you can only use string literals. So no, you can not directly use format!()
. Seems like FLTK does not intend for dynamically allocated strings to be used in this manner.
There is a hack to get around this using Box::leak
. But be aware that it does what it says on the tin - it leaks memory, unless you reclaim it via Box::from_raw()
after the widget is destroyed.
let leaked_title = &*Box::leak(format!("abc {}", 1).into_boxed_str())
fltk::frame::Frame::new(0,0, 300, 100, leaked_title);