Is there any way to return a reference to a variable created in a function?

后端 未结 4 1973
天涯浪人
天涯浪人 2020-11-21 05:03

I want to write a program that will write a file in 2 steps. It is likely that the file may not exist before the program is run. The filename is fixed.

The problem i

4条回答
  •  闹比i
    闹比i (楼主)
    2020-11-21 05:36

    Is there any way to return a reference from a function without arguments?

    No (except references to static values, but those aren't helpful here).

    However, you might want to look at OpenOptions::create. If you change your first line in main to

    let  f = OpenOptions::new().write(true).create(true).open(b"foo.txt");
    

    the file will be created if it does not yet exist, which should solve your original problem.

提交回复
热议问题