Se tem uma coisa que define o Rust, é o conceito de Ownership (Propriedade). Esse conceito é o que permite ao Rust ser seguro e rápido, sem precisar de garbage collector. Vamos entender como isto funciona? O que é Ownership? Em Rust, cada valor tem um dono: a variável que o criou (ou recebeu). Quando essa variável sai de escopo, o valor é automaticamente libertado da memória. fn main() { let nome = String::from("Rust"); println!("Olá, {}", nome); } //

Se tem uma coisa que define o Rust, é o conceito de Ownership (Propriedade).
Esse conceito é o que permite ao Rust ser seguro e rápido, sem precisar de garbage collector. Vamos entender como isto funciona?
O que é Ownership?
Em Rust, cada valor tem um dono: a variável que o criou (ou recebeu). Quando essa variável sai de escopo, o valor é automaticamente libertado da memória.
fn main() {
let nome = String::from("Rust");
println!("Olá, {}", nome);
} // <- 'nome' é destruído aqui, a memória é libertada
Transferência de Posse (Move)
Se atribuír uma variável a outra, o ownership é movido para a nova variável.
fn main() {
let a = String::from("Ownership");
let b = a; // 'a' perde a posse, 'b' agora é o dono
// println!("{}", a); Erro! 'a' não é mais válida aqui
println!("{}", b); // correto
}
Rust faz isso para garantir que nunca existam dois donos ao mesmo tempo — evitando bugs de memória.
O que não se pode fazer?
Não se pode usar uma variável depois de perder a posse do valor.
O compilador vai impedir a compilação se tentar fazê-lo. Pode parecer rígido no início, mas é isso que torna o código tão seguro.
Porque é que isto é importante?
O sistema de ownership permite:
- Segurança de memória sem custos em tempo de execução
- Desempenho comparável ao C/C++
- Código livre de fugas de memória e acessos inválidos
Rust obriga a pensar quem é responsável pela memória — e embora pareça exigente no início, dá muito mais poder e confiança a longo prazo.
If there's one thing that defines Rust, it's the concept of Ownership.
This concept is what allows Rust to be safe and fast, without needing a garbage collector. Let's understand how this works?
What is Ownership?
In Rust, each value has an owner: the variable that created it (or received it). When this variable goes out of scope, the value is automatically freed from memory.
fn main() {
let nome = String::from("Rust");
println!("Hello, {}", nome);
} // <- 'nome' is destroyed here, memory is freed
Ownership Transfer (Move)
If you assign a variable to another, the ownership is moved to the new variable.
fn main() {
let a = String::from("Ownership");
let b = a; // 'a' loses ownership, 'b' is now the owner
// println!("{}", a); Error! 'a' is no longer valid here
println!("{}", b); // correct
}
Rust does this to ensure that there are never two owners at the same time — avoiding memory bugs.
What can't you do?
You can't use a variable after losing ownership of the value.
The compiler will prevent compilation if you try to do so. It might seem rigid at first, but it's what makes the code so safe.
Why is this important?
The ownership system allows:
- Memory safety without runtime costs
- Performance comparable to C/C++
- Code free from memory leaks and invalid accesses
Rust forces you to think about who is responsible for the memory — and although it may seem demanding at first, it gives you much more power and confidence in the long run.