File tree Expand file tree Collapse file tree 1 file changed +8
-4
lines changed
docs/_docs/reference/experimental/capture-checking Expand file tree Collapse file tree 1 file changed +8
-4
lines changed Original file line number Diff line number Diff line change @@ -89,12 +89,12 @@ Consider this example:
8989``` scala
9090def outer (c1 : Cap ^ ) = // level: outer
9191 val x = 1 // level: outer (vals don't create levels)
92- val ref = Ref [ () => Unit ](( ) => () )
92+ var ref : () => Unit = ( ) => ()
9393
9494 def inner (c2 : Cap ^ ) = // level: inner
9595 val y = 2 // level: inner
9696 val f = () => c2.use()
97- ref.set(f) // Error: c2 would escape its level
97+ ref = f // Error: c2 would escape its level
9898
9999 class Local : // level: Local
100100 def method (c3 : Cap ^ ) = // level: method
@@ -127,14 +127,18 @@ capabilities it contains.
127127Consider a ` withFile ` pattern that ensures a file handle doesn't escape:
128128
129129``` rust
130+ struct File ;
131+ impl File { fn open (_path : & str ) -> Option <File > { Some (File ) } }
132+
130133// Rust: the closure receives a reference bounded by 'a
131- fn with_file <' a , R >(path : & str , f : impl FnOnce (& 'a File ) -> R ) -> R {
134+ fn with_file <R >(path : & str , f : impl for <' a > FnOnce (& 'a File ) -> R ) -> R {
132135 let file = File :: open (path ). unwrap ();
133136 f (& file )
134137}
135138
136139fn main () {
137- let escaped : & File ;
140+ let f = File ;
141+ let mut escaped : & File = & f ;
138142 with_file (" test.txt" , | file | {
139143 escaped = file ; // Error: borrowed value does not live long enough
140144 });
You can’t perform that action at this time.
0 commit comments