types - Calling closures from an array in Rust -
How can I be repeated on an array of ports, calling each one in return?
With the task, I have found that I can do this by just turning it on the array, and by removing the values which produce:
fn class (X: int) - & gt; Int {x * x} fn add_one (x: int) - & gt; Int {x + 1} fn main () {let's funcs = [class, add_one]; For Func () in Funcs.iter () println! ("{}", (* Func) (5i));}} However, when I do this with Closer Try, I get an error:
fn main () {close = [| X: int xxx | x + x + 1]; closures.iter () {Println! ("{}", (* Off) (10i));}} produces:
& Lt; anon & gt; 4: 24: 4:34 Error: `& amp;` context & lt; unon & gt ;: 4 printlen! ("{}", (* Ending) (10i)); ^ ~ ~~~~~~~~ Note: in the extension of format_args! & Lt; std macros & gt; 2: 23: 2:77 note: extension site & lt; std macros & gt;: 1: 1: 3 : 2 Note: of println In detail! 4: 9: 4:41 Note: Extension site & lt; anon & gt;: 4: 24: 4:34 note: the situations behind the references & amp; mut` & lt; anon & gt; Should be called via: 4 println! ("{}", (* Closing) (10i)); ^ ~~~~~~~~~ Note: in the extension of format_args! & Lt; std macros & gt; : 2: 23: 2:77 Note: Extension site & lt; std macros & gt;: 1: 1: 3: 2 Note: in the extension of println! 4: 9: 4:41 Note: If I try to declare recurring variable , then it still does not work : fn main () {closed closing = [| X: int | X * x, | X | X + 1]; To turn off vacancies to close. () {Println! ("{}", (* Off) (10i)); }} Results in:
: 4: 24: 4:39 error: expected function, found & amp; | Int | - & gt; Int` & lt; Anon>: 4 println! ("{}", (* Off) (10i)); ^ ~~~~~~~~~~~~~~ Note: in the extension of format_args! & Lt; Std macros & gt ;: 2: 23: 2:77 NOTE: Extension site & lt; Std macros & gt; 1: 1: 3: 2 Note: in the extension of println! 4: 9: 4:41 Note: The site of the extension If I add another dereference:
fn main () {Turn off = [| | X: int | X * x, | X | X + 1]; To stop closing vacancies. () {Println! ("{}", (Closed **) (10i)); }} I returned to the original error:
& lt; Anon>: 4: 24: 4:35 error: `& Amp; & Amp;; Reference & lt; Anon>: 4 println! ("{}", (Closed **) (10i)); ^ ~~~~~~~~~~ Note: in the extension of format_args! & Lt; Std macros & gt ;: 2: 23: 2:77 NOTE: Extension site & lt; Std macros & gt; 1: 1: 3: 2 Note: in the extension of println! 4: 9: 4:42 note: extension site & lt; Anon>: 4: 24: 4:35 note: the back of the referencing is close to the & amp; Mut` & lt; Anon> Should be called through: 4 println! ("{}", (Closed **) (10i)); ^ ~~~~~~~~~~ Note: in the extension of format_args! & Lt; Std macros & gt ;: 2: 23: 2:77 NOTE: Extension site & lt; Std macros & gt; 1: 1: 3: 2 Note: in the extension of println! & Lt; Anon> 4: 9: 4:42 note: extension site What am I missing here? Is there a document that describes how it works?
.iter () The method of irreversible references to vector yield, you are temporary People need to call to stop, so you should use .iter_mut () : fn main () Close = [| X: int | X * x, | X | X + 1]; Closures.iter_mut () {println! ("{}", (* Closing) (10i)) to stop; }} ----- 100 11
Comments
Post a Comment