I am a freshman in Rust and have studied it for 3 months. I think Rust is well designed in most aspects. But it's hard for a freshman like me to understand and take master of ownership and liftetime management. I can understand it is designed to make sure pointer is released if it is not used any more. To make it easier for the poor people who is new to Rust, I designed another way to make sure pointer is released saftly without ownership and lifetime.
Here are the advices:
1. Add a new word 'with' in Rust. The function with is just like with in Python or Using in c# or try...with...resources in java, it automaticly release pointer at the end of with block.
2. Add a special type of pointer which need to be returned, let's call it 'ReturnPointer'.
3. The Rule is, if you want use normal pointer, you must define it as 'with block', it will be auto matically released at the end of block; If you want to return a pointer, you should define it as 'ReturnPointer' type; 'ReturnPointer' can be converted to normal pointer, but if you do that, you must convert it as with block.
I hope this can help Rust more easier to learn. Any ideas guys?
I think what you want in Rust would be spelled simply as a block scope { ... }, anything declared in that scope will be released at the end, the last expression in that scope will be returned to the outer scope.
Anyway, discussions about the language itself are generally redirectered to IRLO.
The with statement exists in Python so you can clean up a value when it goes out of scope, instead of at some random point in time when the garbage collector runs. Rust already cleans up all variables immediately when they go out of scope, so I don't really understand the point.
Yes,you are right the normal value will be released out of scope, but I am focus on the pointer release problem which rust specially designed ownership and lifetime for it. I am thinking of a simple way to replace the ownership and lifetime, make rust easier.
发自我的手机
发件人: Alice Ryhl via The Rust Programming Language Forum notifications@rust-lang.discoursemail.com
日期: 2022年9月6日周二 晚上9:56
收件人: newtome1111@163.com
主 题: [rust-users] [community] A context manager to handle pointer without
ownership and lifetime trouble
I don't think adding context managers to the language would help much here seeing as Rust's deterministic destructors are a strict superset of tools like Python's with or C#'s using[1].
As an example, I can implement a using statement by just creating a new scope and assigning the value to a variable (please excuse the weird syntax - it's just the first using-like syntax I thought of).
macro_rules! using {
(var $name:ident = $initializer:expr => $body:expr) => {{
let $name = $initializer;
$body;
}};
}
fn main() {
using!(var a = PrintOnDrop(0) => {
using!(var b = PrintOnDrop(1) => {
println!("{}, {}", a.0, b.0);
});
println!("Destructors are still called during a panic");
panic!();
});
}
struct PrintOnDrop(usize);
impl Drop for PrintOnDrop {
fn drop(&mut self) {
println!("Dropped {}!", self.0);
}
}
If the actual issue is wanting to simplify Rust's learning curve, then I think time might be better spent creating better teaching resources than trying to rewrite/remove a core language mechanic.
Also note that this will become easier over time. Rust has been around long enough and gained enough popularity that universities are starting to teach it alongside mainstays like C, Python, and Java. That means we'll be getting new generations of programmers that have already been exposed to the concept of ownership and lifetimes instead of people graduating, entering industry, and being thrown in the deep end with something that works quite differently to what they were taught.
Destructors are called at the end of a scope even if an exception/panic occurs, you can also pass it around as a value to control a resource's lifetime at runtime, etc. ↩︎
I think you are right, the current designation of Rust destructors doesn't suport context, what I mean is to redesign the destructor. But I can understand that it's diffcult, because it means rewrite the core of Rust. I like the simple way, but maybe I should design another language myself, cause I really think ownership and lifetime should be simplified.