borrowing and referrencing day one

This commit is contained in:
Rezon Philip 2026-03-09 01:25:46 +05:30
parent ec76bc9dbd
commit cbca7e05bf
1 changed files with 18 additions and 45 deletions

View File

@ -1,54 +1,27 @@
fn main() {
// This is an example of variable shadowing
let _variable_one = "new string";
let _variable_one = _variable_one.to_owned() + "second string";
println!("Hello, world!");
let _array_one: [i32; 4] = [1,3,4,5];
let _arrays = [4,5,45,5,5,5];
let _tuple_one = (3,5,5,"fdsfd");
let _array_pointer: &[i32] = &[1,3,4,5,6,6];
let string_slice : &[String] = &["test string one ".to_string(), "this is the second string".to_string()];
println!("this is a reference to a string array {:?}", string_slice);
let mut string_owned :String = String::from("hello rust");
let string_borrowed: &str = &string_owned;
println!("this is the borrowed string {}", string_borrowed);
string_owned.push_str("string");
println!("this is the borrowed string value now {}", string_owned);
let x = {
let first: i32 = 67;
let second = 54;
first * second
let mut rezon_account = BackAccount {
name : "riZzon".to_string(),
balance : 6969.68,
};
let y = 56;
println!("the is the value of the expression evaluated: {:?}", x);
println!("this is the value of a function expression evaluating to {:?}", add(x, y));
let string_one_owner : String = String::from("im the ownee");
println!("The size of string {} is {}.", string_one_owner, get_string_len(&string_one_owner));
let _string_two_one_owner = string_one_owner;
println!("this is the original owner : {}", string_one_owner);
println!("this is the new owner : {}", _string_two_one_owner);
rezon_account.check_balance();
rezon_account.withdraw(34.4);
rezon_account.check_balance();
}
fn add (first :i32, second: i32) -> i32 {
first * second
struct BackAccount {
name: String,
balance: f32,
}
fn get_string_len(str :&String) -> usize {
str.len()
impl BackAccount {
fn check_balance(&self) {
println!("the user {}, has a current account balance of {}", self.name, self.balance)
}
fn withdraw(&mut self, amount: f32) {
println!("withdrawing an amount of {}, from {} user's balance ", amount, self.name);
self.balance -= amount
}
}