mirror of
https://github.com/rust-lang/rustlings.git
synced 2025-12-28 14:59:18 +00:00
翻譯
This commit is contained in:
parent
798a2f03ae
commit
5d717537fe
@ -1,22 +1,12 @@
|
||||
# Lifetimes
|
||||
# 生命週期
|
||||
|
||||
Lifetimes tell the compiler how to check whether references live long
|
||||
enough to be valid in any given situation. For example lifetimes say
|
||||
"make sure parameter 'a' lives as long as parameter 'b' so that the return
|
||||
value is valid".
|
||||
生命週期用於告訴編譯器如何檢查引用是否有足夠長的存續時間,以確保它們有效。例如,生命週期可以說「確保參數 'a' 的存活時間至少與參數 'b' 一樣長,這樣回傳的值才是有效的」。
|
||||
|
||||
They are only necessary on borrows, i.e. references,
|
||||
since copied parameters or moves are owned in their scope and cannot
|
||||
be referenced outside. Lifetimes mean that calling code of e.g. functions
|
||||
can be checked to make sure their arguments are valid. Lifetimes are
|
||||
restrictive of their callers.
|
||||
它們僅對借用(即引用)是必要的,因為被複製的參數或移動的參數在它們的範圍內是擁有的,不能在範圍外引用。生命週期意味著可以檢查函數等的調用代碼,以確保它們的參數是有效的。生命週期對其調用者是有約束力的。
|
||||
|
||||
If you'd like to learn more about lifetime annotations, the
|
||||
[lifetimekata](https://tfpk.github.io/lifetimekata/) project
|
||||
has a similar style of exercises to Rustlings, but is all about
|
||||
learning to write lifetime annotations.
|
||||
如果您想了解更多關於生命週期註釋的資訊,[lifetimekata](https://tfpk.github.io/lifetimekata/) 項目提供了一種類似於 Rustlings 的練習風格,但全都是關於學習如何編寫生命週期註釋。
|
||||
|
||||
## Further information
|
||||
## 進一步了解
|
||||
|
||||
- [Lifetimes (in Rust By Example)](https://doc.rust-lang.org/stable/rust-by-example/scope/lifetime.html)
|
||||
- [Validating References with Lifetimes](https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html)
|
||||
- [使用生命週期驗證引用](https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html)
|
||||
|
||||
@ -1,12 +1,8 @@
|
||||
// lifetimes1.rs
|
||||
//
|
||||
// The Rust compiler needs to know how to check whether supplied references are
|
||||
// valid, so that it can let the programmer know if a reference is at risk of
|
||||
// going out of scope before it is used. Remember, references are borrows and do
|
||||
// not own their own data. What if their owner goes out of scope?
|
||||
// Rust編譯器需要知道如何檢查提供的引用是否有效,以便讓程式設計師知道引用是否有在使用前超出範圍的風險。記住,引用是借用,不擁有它們自己的值。如果它們的所有者超出範圍怎麼辦?
|
||||
//
|
||||
// Execute `rustlings hint lifetimes1` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
// 執行 `rustlings hint lifetimes1` 或使用 `hint` watch 子命令來獲取提示。
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
|
||||
@ -1,10 +1,8 @@
|
||||
// lifetimes2.rs
|
||||
//
|
||||
// So if the compiler is just validating the references passed to the annotated
|
||||
// parameters and the return type, what do we need to change?
|
||||
// 如果編譯器只是驗證傳遞給註釋參數和回傳類型的引用,那麼我們需要更改什麼?
|
||||
//
|
||||
// Execute `rustlings hint lifetimes2` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
// 執行 `rustlings hint lifetimes2` 或使用 `hint` watch 子命令來獲取提示。
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
|
||||
@ -1,9 +1,8 @@
|
||||
// lifetimes3.rs
|
||||
//
|
||||
// Lifetimes are also needed when structs hold references.
|
||||
// 當結構體持有引用時,也需要生命週期標註。
|
||||
//
|
||||
// Execute `rustlings hint lifetimes3` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
// 執行 `rustlings hint lifetimes3` 或使用 `hint` watch 子命令來獲取提示。
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user