January 2019
Intermediate to advanced
520 pages
14h 32m
English
To render a template of an index page, we use a struct with a String field and we have to fill the IndexTemplate struct to call the render method on it. But the template needs an ownership for the value and we have to clone it. Cloning in turn takes time. To avoid this CPU cost, we can use a reference to a value, because if we clone a value that uses a memory heap, we have to allocate a new memory space and copy bytes of the value to a new place.
This is how we can add a reference to a value:
struct IndexTemplate<'a> { // time: String, time: &'a str,}
We have added the 'a lifetime to a struct, because we use a reference inside and the struct that can't live longer than the string value we referred to.
Using references ...
Read now
Unlock full access