Rust return a font (rusttype::Font) from a function

So I have this function:

fn retrieve_font(url: String) -> Result<Font<'static>, anyhow::Error> {
    let font_data = reqwest::blocking::get(url)?

    let font = Font::try_from_bytes(&*font_data);

    match font {
        None => {
            bail!("Invalid font")
        Some(f) => { Ok(f.to_owned()) }

Upon compiling, I got

96 |         Some(f) => { Ok(f.to_owned()) }
   |                      ^^^^^^^^^^^^^^^^ returns a value referencing data owned by the current functio

Now I understand that this is due to me trying to return the reference of a value created and owned by the function.

However, I just want to load the Font (from rusttype) :sob:.
I guess I can return a vec[u8], but then every time I want to use the font, I have to do the let font = Font::try_from_bytes(&*font_data);, which I am not sure is good for performance.

Do I have any other choices for returning a Font from a function?
Thank you.

What library does the Font type come from?

Sorry my bad. It is from rusttype.

Use the try_from_vec method instead of try_from_bytes.

1 Like