我有下面的代码,但不知道如何让它工作:
fn new_int<'a>() -> &'a isize {
&5
}
fn main() {
let x = new_int();
}
或者另一种尝试:
fn new_int<'a>() -> &'a isize {
let a: &'a isize = &5;
a
}
fn main() {
let x = new_int();
}
new_int
)可以通过两种方式分配内存:&
)是指向内存区域的指针。它可以指向本地栈或堆。由于动态分配在性能方面的成本要比写入栈高得多,因此Rust默认使用栈(您必须使用Box来执行动态分配)。fn new_int<'a>() -> &'a isize {
let a: &'a isize = &5; // write 5 on the function's local stack
a // return a pointer to that area of memory
} // the function ends and its stack (where I wrote 5) is destroyed
// so the pointer I'm trying to return is no longer valid
你可以返回值
fn new_int() -> isize {
5
}
fn main() {
let a = new_int(); // the value 5 (not a pointer) is copied into a
}
或者执行动态分配(如果您实际上正在使用大型结构,则 isize 可能过于复杂,但可能是有意义的)
fn new_int() -> Box<isize> {
Box::new(5) // a Box allocates memory and writes in the heap
}
fn main() {
let a = *new_int();
}
另外,您可以在函数外部分配内存,并在函数中进行修改。对于原始类型,通常不这样做,但在某些情况下(例如数据流),这是有意义的:
// new_int does not return anything. Instead it mutates
// the old_int in place
fn new_int(old_int: &mut isize) {
*old_int = 5;
}
fn main() {
let mut a = 2; // memory for an int is allocated locally
// in main()
new_int(&mut a); // a mutable reference to that memory is passed
// to new_int, that overwrites it with another value
}
fn new_int<'a>() -> &'a isize {
static FIVE: isize = 5;
&FIVE
}
static
在 Rust 参考手册中' 的更多信息。static FIVE
的内容。5
,或者任何有限的一组答案,您可以返回对静态变量的借用引用:static FIVE: isize = 5;
,然后将&FIVE
作为函数的结果。 - DK.fn new_int<'a>() -> &'a isize {
&5
}
'a
是函数的生命周期参数;也就是说,实际值由调用方选择,而不是函数本身。例如,调用方可以选择'static
生命周期:let i: &'static isize = new_int();
然而,&5
不能拥有'static
生命周期,因此该函数被拒绝。
换句话说,这种声明本质上是在说“我可以给你一个你想要的任何生命周期的引用”。当然,只有当函数返回的引用是'static
生命周期时才有效,这是可能的最长寿命。顺便说一下,这就是DK.所说的。
生命周期仅描述代码已经执行的操作,不会以任何方式影响代码的行为。
它们不是指示将某些东西保持活动状态所需的指令,而是一致性检查,确保代码实际执行其所说的内容。
实际上,Rust在检查完生命周期之后会从代码中删除所有生命周期信息,然后编译代码而没有任何关于生命周期的知识。
变量在其作用域结束时被销毁,这就是它们的生命周期。您不能声明它们不会这样做。
static
提升(请参见为什么我可以返回对本地文字的引用,但不能返回变量?),这些函数现在已经编译成功。 - trent