1. Home
  2. /
  3. Rust
  4. /
  5. Ownership Example

Ownership Example - Rust Typing CST Test

Loading…

Ownership Example — Rust Code

Demonstrates Rust's ownership system, iterators, and struct implementation with testing.

fn main() {
	let mut numbers = vec![1, 2, 3, 4, 5];

	println!("Original: {:?}", numbers);

	let doubled: Vec<i32> = numbers.iter().map(|x| x * 2).collect();
	println!("Doubled: {:?}", doubled);

	let sum: i32 = numbers.iter().sum();
	println!("Sum: {}", sum);

	numbers.push(6);
	println!("Modified: {:?}", numbers);
}

#[derive(Debug)]
struct Rectangle {
	width: u32,
	height: u32,
}

impl Rectangle {
	fn area(&self) -> u32 {
		self.width * self.height
	}
}

#[test]
fn test_rectangle_area() {
	let rect = Rectangle { width: 10, height: 20 };
	assert_eq!(rect.area(), 200);
}

Rust Language Guide

A modern, memory-safe, high-performance systems programming language focused on safety, concurrency, and zero-cost abstractions, designed to replace C/C++ in critical software.

Primary Use Cases

  • ▸Systems programming
  • ▸WebAssembly applications
  • ▸Cloud-native backends
  • ▸Blockchain and cryptographic systems
  • ▸Embedded systems
  • ▸Game engines
  • ▸High-performance CLI tools

Notable Features

  • ▸Memory safety without garbage collection
  • ▸Ownership and borrowing system
  • ▸Zero-cost abstractions
  • ▸Powerful package manager (Cargo)
  • ▸Pattern matching and algebraic data types
  • ▸Fearless concurrency

Origin & Creator

Created by Graydon Hoare at Mozilla Research, first public release in 2010, and stable 1.0 released in 2015. Originated as a personal project, later backed by Mozilla. Evolved through Rust 2015, 2018, and 2021 editions, adding async/await, improved ergonomics, better tooling, Cargo, and a richer standard library.

Industrial Note

Highly adopted in cloud infrastructure, operating system components, cryptography, blockchain systems, safety-critical systems, and WebAssembly. Used by Microsoft, Amazon, Google, Cloudflare, and Meta for secure and high-performance services.

Quick Explain

  • ▸Rust is a compiled, multi-paradigm language emphasizing memory safety without garbage collection.
  • ▸It uses ownership, borrowing, and lifetimes to provide deterministic safety at compile time.
  • ▸Designed for system programming, WebAssembly, cloud services, and performance-critical applications.

Core Features

  • ▸Ownership, borrowing, lifetimes
  • ▸Traits and generics
  • ▸Pattern matching
  • ▸Async/await
  • ▸Smart pointers and interior mutability

Learning Path

  • ▸Learn ownership and borrowing
  • ▸Understand enums and pattern matching
  • ▸Master traits and generics
  • ▸Learn async Rust
  • ▸Build real applications

Practical Examples

  • ▸Build a CLI tool using clap
  • ▸Create a REST API with Axum/Actix
  • ▸Write a WebAssembly module
  • ▸Develop a multithreaded task scheduler

Comparisons

  • ▸Safer than C/C++
  • ▸Faster than Go in many workloads
  • ▸More predictable than Java
  • ▸Lower-level than Python

Strengths

  • ▸Eliminates common memory bugs
  • ▸Performance comparable to C++
  • ▸Excellent developer experience with Cargo
  • ▸Strong compile-time guarantees
  • ▸Growing community and enterprise adoption

Limitations

  • ▸Steep learning curve due to ownership model
  • ▸Long compile times
  • ▸More complex generics and lifetimes
  • ▸Smaller ecosystem than C++/Python

When NOT to Use

  • ▸When rapid prototyping is needed
  • ▸For simple scripts or automation
  • ▸When large VM-based ecosystems are required
  • ▸When compile times are a bottleneck

Cheat Sheet

  • ▸Ownership rules
  • ▸Common lifetimes
  • ▸Trait implementations
  • ▸Cargo command patterns

FAQ

  • ▸Why is Rust so safe?
  • ▸Rust prevents memory bugs at compile time using ownership, borrowing, and lifetimes, eliminating entire classes of errors without garbage collection.
  • ▸Is Rust good for beginners?
  • ▸Rust is challenging but rewarding; excellent for those wanting to learn safe, modern systems programming.
  • ▸How do I avoid borrow checker errors?
  • ▸Follow ownership guidelines, avoid unnecessary mutable references, and break logic into smaller functions.
  • ▸How is Rust different from C++?
  • ▸Rust guarantees safety and concurrency without garbage collection, whereas C++ relies on manual discipline and conventions.

30-Day Skill Plan

  • ▸Week 1: Ownership, lifetimes, basic syntax
  • ▸Week 2: Collections, traits, generics
  • ▸Week 3: Async Rust, threading
  • ▸Week 4: Build a full backend or CLI project

Final Summary

  • ▸Rust provides memory safety and high performance.
  • ▸Ideal for systems programming, WASM, and secure backends.
  • ▸Cargo and the ecosystem make development smooth.
  • ▸Mastering Rust prepares you for modern software engineering.

Project Structure

  • ▸src/ for main code
  • ▸Cargo.toml for manifest
  • ▸target/ for compiled binaries
  • ▸tests/ for integration tests
  • ▸examples/ for demo programs

Monetization

  • ▸Build secure cloud tools
  • ▸Develop blockchain protocols
  • ▸Sell CLI utilities and libraries

Productivity Tips

  • ▸Use cargo-watch
  • ▸Master pattern matching
  • ▸Use Result everywhere for safety
  • ▸Leverage crates.io effectively

Basic Concepts

  • ▸Ownership and borrowing
  • ▸Variables and shadowing
  • ▸Functions and closures
  • ▸Structs and enums
  • ▸Traits and generics
  • ▸Error handling with Result/Option

Official Docs

  • ▸The Rust Programming Language book
  • ▸Rust Reference
  • ▸Rustonomicon

More Rust Typing Exercises

Rust Borrowing ExampleRust Trait ExampleRust Enum ExampleRust Result and Error HandlingRust Iterator ExampleRust Struct with MethodsRust String ManipulationRust Generic FunctionRust Closures Example

Practice Other Languages

CReactPythonC++TypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypherGremlin