Sorry friend, I need some help. I don't understand why drop()
cannot remove the compile error:
#![allow(unused_variables, dead_code)]
use std::rc::Rc;
fn foo(rc: &Rc<u8>) {
()
}
#[tokio::main]
async fn main() {
tokio::spawn(async {
let v = Rc::new(42u8);
// foo(&v); // why uncomment this line will cause compile error?
drop(v); // ... and `v` never across an await point
async{1}.await;
});
}
And the following code can work as expect:
#![allow(unused_variables, dead_code)]
use std::rc::Rc;
fn foo(rc: &Rc<u8>) {
()
}
#[tokio::main]
async fn main() {
tokio::spawn(async {
{
let v = Rc::new(42u8);
foo(&v);
} // use block syntax to replace `drop(v)` and it work... but why?
async{1}.await;
});
}
And I also check drop()
effective in almost the same scenario.
#![allow(unused_variables, dead_code)]
use std::rc::Rc;
fn foo(rc: &Rc<u8>) {
()
}
#[tokio::main]
async fn main() {
tokio::spawn(async {
let v = Rc::new(42u8);
drop(v); // comment this line make compile error so drop is effective.
async{1}.await;
});
}
Can someone give me an explain? Thanks.