调用2个函数:首先返回值,然后赋值

时间:2019-07-23 12:11:00

标签: r

我有两个职能。我需要调用一个函数并返回一个值,但是然后我想调用第二个函数。我无法执行不返回任何内容的函数,然后针对时间问题执行第二个函数

error[E0515]: cannot return value referencing local variable `dispatcher`
  --> src/lib.rs:46:9
   |
44 |           example.init(&mut dispatcher);
   |                        --------------- `dispatcher` is borrowed here
45 | 
46 | /         Handler {
47 | |             example: example,
48 | |             dispatcher: dispatcher
49 | |         }
   | |_________^ returns a value referencing data owned by the current function

error[E0515]: cannot return value referencing local variable `example`
  --> src/lib.rs:46:9
   |
44 |           example.init(&mut dispatcher);
   |           ------- `example` is borrowed here
45 | 
46 | /         Handler {
47 | |             example: example,
48 | |             dispatcher: dispatcher
49 | |         }
   | |_________^ returns a value referencing data owned by the current function

error[E0505]: cannot move out of `example` because it is borrowed
  --> src/lib.rs:47:22
   |
39 |   impl<'a> Handler<'a> {
   |        -- lifetime `'a` defined here
...
44 |           example.init(&mut dispatcher);
   |           ------- borrow of `example` occurs here
45 | 
46 | /         Handler {
47 | |             example: example,
   | |                      ^^^^^^^ move out of `example` occurs here
48 | |             dispatcher: dispatcher
49 | |         }
   | |_________- returning this value requires that `example` is borrowed for `'a`

error[E0505]: cannot move out of `dispatcher` because it is borrowed
  --> src/lib.rs:48:25
   |
39 |   impl<'a> Handler<'a> {
   |        -- lifetime `'a` defined here
...
44 |           example.init(&mut dispatcher);
   |                        --------------- borrow of `dispatcher` occurs here
45 | 
46 | /         Handler {
47 | |             example: example,
48 | |             dispatcher: dispatcher
   | |                         ^^^^^^^^^^ move out of `dispatcher` occurs here
49 | |         }
   | |_________- returning this value requires that `dispatcher` is borrowed for `'a`

1 个答案:

答案 0 :(得分:1)

您想做的事情是不可能的,但是您可以先打印要返回的值,然后调用第二个函数,然后使用invisible()静默返回该值。像这样:

segunda <- function(msg){
  number <- 0
}


primera <- function(msg){
  s <- paste(msg, " 1 ")
  print(s)
  segunda(msg)
  invisible(s)
}

如果您在控制台中进行评估:

x <- primera("test")

然后"test 1"仅在评估之前 segunda(msg)打印到控制台,但结果仍分配给x