I am new to the language and still struggling with the borrower. I saw that some libraries use new () aka constructor functions with no parameters, and this works. This basically means that the returned data is created inside the new function area and is not deleted at the end of the new area.
When trying this, the borrower himself does not skip this code. How to do this job, except passing the i32 mutable link as a parameter to the constructor.
Am I missing something?
Compiler error.
main.rs:15:19: 15:24 error: borrowed value does not live long enough main.rs:15 one: B{b: &mut 10i32} ^~~~~ main.rs:12:20: 17:3 note: reference must be valid for the lifetime 'a as defined on the block at 12:19... main.rs:12 fn new() -> A<'a> { main.rs:13
As requested, here is a practical example I'm trying to work with. There is this GUI library (Conrod), and it has some steps to create it. As in the example below.
let assets = find_folder::Search::ParentsThenKids(3, 3) .for_folder("assets").unwrap(); let font_path = assets.join("fonts/NotoSans/NotoSans-Regular.ttf"); let theme = Theme::default(); let glyph_cache = GlyphCache::new(&font_path).unwrap(); let ui = &mut Ui::new(glyph_cache, theme);
My plan was to encapsulate the application drawing into a structure. This will have a constructor and several helper methods. To do this, I must have a field with an instance of type conrod::Ui<GlyphCache<'a>>
, which is the type for the ui variable above.
I think adding things to the main (I mean all the distributions made basically) may not be the best way to do something.
let mut app_ui = app::AppUi::new();
Implementation of AppUi. This is not complete, but should show a general idea. To make sure that we are on the same page, the type conrod::Ui<GlyphCache<'a>>
requires a lifetime parameter. And I want to have the same life as the structure. The only way I know how to do this is to make struct get the life parameter itself and pass it to the user interface type.
pub struct AppUi<'a> { pub ui: conrod::Ui<GlyphCache<'a>>, pub count: u16 } impl<'a> AppUi<'a> { pub fn new() -> AppUi<'a> { let assets = find_folder::Search::ParentsThenKids(3, 3) .for_folder("assets").unwrap(); let font_path = assets.join("FiraSans-Regular.ttf"); let theme = Theme::default(); let glyph_cache = GlyphCache::new(&font_path).unwrap(); AppUi { ui: conrod::Ui::new(glyph_cache, theme), count: 0 } } }
=========================
The solution I went with worked, and it worked at the end (at least it works now). A helper function was created that would return the glyph cache and just use it. I'm not sure if it will be idiomatic Rust, just use it for now. You must be used to working with the borrower.
pub struct AppUi<'a> { pub ui: conrod::Ui<GlyphCache<'a>>, pub count: u16 } impl<'a> AppUi<'a> { pub fn new() -> AppUi<'a> { AppUi { ui: conrod::Ui::new(GlyphCache::new(&get_default_font_path()).unwrap(), Theme::default()), count: 0 } } } pub fn get_default_font_path() -> PathBuf { find_folder::Search::ParentsThenKids(3, 3) .for_folder("assets") .unwrap() .join("FiraSans-Regular.ttf") }