0Pricing
Learn Rust Coding · Ders

Yaşam Süresi Açıklamaları

Yaşam sürelerini adlandırma

Yaşam Süresi Açıklamaları, CoddyKit'te ücretsiz bir Learn Rust Coding dersidir. Bu, 4 dersinin 2. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Learn Rust Coding öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Learn Rust Coding kursu toplamda 4 dersten oluşur.

Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.

Naming a Lifetime

A lifetime annotation is a name starting with an apostrophe, like 'a. It does not change how long anything lives; it describes relationships between the lifetimes of references.

Where Annotations Go

You declare lifetime parameters in angle brackets after the function name, then use them on the reference types, just like generic type parameters.

Syntax: fn name<'a>(x: &'a T) -> &'a T.

The Classic longest Function

A function returning one of two references needs an annotation. 'a says the result lives as long as the shorter of the two inputs.

fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
    if x.len() > y.len() { x } else { y }
}

fn main() {
    let a = String::from("long string");
    let b = String::from("short");
    println!("{}", longest(&a, &b));
}

What 'a Means Here

The annotation tells the compiler: the returned reference is valid only while both inputs are valid. The compiler then checks every call site against this contract.

Why It Is Needed

Without the annotation, the compiler cannot know whether the return borrows from x or y. The lifetime name links them so the borrow checker can reason about the result.

Different Lifetimes

When references are unrelated, give them different lifetime names. Here only x is returned, so only its lifetime matters for the result.

fn first<'a, 'b>(x: &'a str, _y: &'b str) -> &'a str {
    x
}

fn main() {
    let a = String::from("keep me");
    let b = String::from("ignore");
    println!("{}", first(&a, &b));
}

Lifetimes Do Not Extend Life

Annotations never make data live longer. They only state constraints the compiler must verify. If a value dies too soon, the code will not compile regardless of annotations.

A Valid Call

As long as both inputs outlive the use of the result, the call is accepted. Here both strings live through the print, so all is well.

fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
    if x.len() > y.len() { x } else { y }
}

fn main() {
    let a = String::from("abcdef");
    let result;
    {
        let b = String::from("xy");
        result = longest(&a, &b);
        println!("chosen: {}", result);
    }
}

Lifetimes With Generics

Lifetime and type parameters can appear together. Lifetimes are listed first inside the angle brackets.

use std::fmt::Display;

fn announce<'a, T: Display>(text: &'a str, value: T) -> &'a str {
    println!("value is {}", value);
    text
}

fn main() {
    let msg = String::from("hello");
    println!("{}", announce(&msg, 42));
}

The 'static Lifetime

'static is a special lifetime meaning the reference can live for the entire program. String literals have it because they are baked into the binary.

fn motto() -> &'static str {
    "fearless concurrency"
}

fn main() {
    println!("{}", motto());
}

Reading Annotations

Read &'a str as a string reference valid for lifetime 'a. When two parameters share 'a, the compiler ties their lifetimes to whichever is shorter at each call.

Quick Check

Test your understanding of lifetime annotations.

Recap

You learned to name lifetimes:

  • Lifetimes use names like 'a, declared in angle brackets
  • Shared names express that references relate (e.g. result tied to inputs)
  • Annotations describe, never extend, lifetimes
  • 'static means valid for the whole program

Sıkça Sorulan Sorular

“Yaşam Süresi Açıklamaları” dersi ücretsiz mi?

Evet — “Yaşam Süresi Açıklamaları” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Learn Rust Coding kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Learn Rust Coding kursu toplamda 4 dersten oluşur.

“Yaşam Süresi Açıklamaları” dersinde ne öğreneceğim?

Yaşam sürelerini adlandırma Learn Rust Coding ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.

Learn Rust Coding öğrenmeye başlamak için deneyim gerekli mi?

Önceden deneyim gerekmez. CoddyKit'te Learn Rust Coding, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 2. dersidir.

“Yaşam Süresi Açıklamaları” dersi ne kadar sürer?

Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.

Bu Learn Rust Coding dersinde kod yazıp çalıştırabilir miyim?

Evet. Her Learn Rust Coding dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.

Bu kursun tüm dersleri

  1. Yaşam Süreleri Neden Gerekli
  2. Yaşam Süresi Açıklamaları
  3. Yapılarda Yaşam Süreleri
  4. Çıkarma Kuralları
← Learn Rust Coding Sayfasına Dön