🕵️ SicherheitslückenHak5: Hackers Just Poisoned the Rust Supply Chain | Threat Wire(01.09.2026 um 14:00 Uhr)
🕵️ SicherheitslückenHak5: Hackers Found a Way Into Humanoid Robots | Threat Wire(04.09.2026 um 15:04 Uhr)
🔧 AI Nachrichten Bits und so #1021 (Passwort für Laufwerk)(31.08.2026 um 22:15 Uhr)
🔧 AI Nachrichten Bits und so #1022 (Wie Weißbier)(06.09.2026 um 20:39 Uhr)
🍏 iOS / Mac OSHue-App 6.0 ist da: das sind die Neuerungen(07.09.2026 um 17:21 Uhr)
🕵️ SicherheitslückenHak5: Hackers Just Poisoned the Rust Supply Chain | Threat Wire(01.09.2026 um 14:00 Uhr)
🕵️ SicherheitslückenHak5: Hackers Found a Way Into Humanoid Robots | Threat Wire(04.09.2026 um 15:04 Uhr)
🔧 AI Nachrichten Bits und so #1021 (Passwort für Laufwerk)(31.08.2026 um 22:15 Uhr)
🔧 AI Nachrichten Bits und so #1022 (Wie Weißbier)(06.09.2026 um 20:39 Uhr)
🍏 iOS / Mac OSHue-App 6.0 ist da: das sind die Neuerungen(07.09.2026 um 17:21 Uhr)

🔧 Programmierung 🕛 kürzlich 3 Min Lesezeit
0

🚀 Rust Basics 2: Data Types, Shadowing, and String Manipulation 🦀

↗ Quelle (dev.to)
🗣️ Stimme:

Welcome back to our RUST BASICS series! In this post, we’ll explore data types, shadowing, and string manipulation in Rust. These concepts will help you understand how Rust handles values and strings efficiently.






Step 1: Understanding Data Types



Rust is a statically-typed language, which means every variable must have a type. Rust can infer types for you, but sometimes it’s useful to specify them explicitly.



Primitive Types in Rust:




  1. Integer: i8, i16, i32, i64, i128 (signed), and u8, u16, u32, u64, u128 (unsigned).


  2. Floating-point: f32 and f64.


  3. Boolean: bool (true or false).


  4. Character: char (single characters like 'a' or '#').




Example Code:




CODE
fn main() {
let age: i32 = 25; // Integer
let height: f64 = 5.9; // Floating-point
let is_student: bool = true; // Boolean
let grade: char = 'A'; // Character

println!("Age: {}", age);
println!("Height: {} ft", height);
println!("Is Student: {}", is_student);
println!("Grade: {}", grade);
}










Step 2: Constants vs Variables



Constants are immutable and must have a type specified. They’re declared with the const keyword and are accessible globally.



Variables (declared with let) can be mutable if prefixed with mut.



Example Code:




CODE
const PI: f64 = 3.14159; // Constant
fn main() {
let mut counter = 0; // Mutable variable
println!("Counter: {}", counter);

counter += 1; // Increment counter
println!("Updated Counter: {}", counter);

println!("Value of PI: {}", PI);
}










Step 3: Shadowing



Shadowing allows you to redefine a variable with the same name. This is useful when you want to transform data without changing its name.



Example Code:




CODE
fn main() {
let number = 5;
println!("Number: {}", number);

let number = number + 1; // Shadowing
println!("Shadowed Number: {}", number);

let number = "Five"; // Shadowing with a different type
println!("Number as a string: {}", number);
}






Note: Shadowing creates a new variable and doesn’t modify the previous one. This is different from using mut.






Step 4: String Manipulation



Rust has two types of strings:




  1. String slices (&str): Immutable references to string data.


  2. String: A growable, heap-allocated data structure.




Example Code:




CODE
fn main() {
let greeting = "Hello"; // &str
let mut name = String::from("TheACJ"); // String

println!("Greeting: {}", greeting);
println!("Name: {}", name);

// Modify the String
name.push_str(" Rustacean!");
println!("Updated Name: {}", name);
}







String Methods:




CODE
fn main() {
let name = String::from("Rustacean");
println!("Length: {}", name.len()); // Get length
println!("Is Empty: {}", name.is_empty()); // Check if empty
println!("Uppercase: {}", name.to_uppercase()); // Convert to uppercase
}










Step 5: Practice Challenges



A. Modify the program to calculate the area of a circle:




CODE
const PI: f64 = 3.14159;
fn main() {
let radius: f64 = 7.0;
let area = PI * radius * radius;
println!("The area of the circle is: {}", area);
}






B. Write a program to count the number of characters in a user-inputted string:




CODE
use std::io;

fn main() {
let mut input = String::new();
println!("Enter a string:");

io::stdin()
.read_line(&mut input)
.expect("Failed to read input");

let trimmed = input.trim();
println!("The string '{}' has {} characters.", trimmed, trimmed.len());
}









Troubleshooting Tips



If you get a type mismatch error, ensure variable types are consistent.



If you see cannot borrow as mutable, check if your variable is declared as mut.






🎉 That’s it for RUST BASICS 2! You’ve learned about data types, constants, shadowing, and string manipulation. In the next post, we’ll cover control flow and loops in Rust.






Feel free to share your solutions or ask questions in the comments. Happy coding! 🦀



Check out the Rust Documentation for more details.

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
Hackers Just Poisoned the Rust Supply Chain | Threat Wire
1 Quelle
Hackers Found a Way Into Humanoid Robots | Threat Wire
1 Quelle
Bits und so #1021 (Passwort für Laufwerk)
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten 🚀 Rust Basics 2: Data Types, Shadowing, and String Manipulation 🦀

Thematisch verwandte Begriffe: Rust, Basics, Data, Types · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...