borrowing and referrencing day one
This commit is contained in:
parent
ec76bc9dbd
commit
cbca7e05bf
63
src/main.rs
63
src/main.rs
|
|
@ -1,54 +1,27 @@
|
||||||
fn main() {
|
fn main() {
|
||||||
|
let mut rezon_account = BackAccount {
|
||||||
// This is an example of variable shadowing
|
name : "riZzon".to_string(),
|
||||||
let _variable_one = "new string";
|
balance : 6969.68,
|
||||||
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 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 {
|
struct BackAccount {
|
||||||
first * second
|
name: String,
|
||||||
|
balance: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_string_len(str :&String) -> usize {
|
impl BackAccount {
|
||||||
str.len()
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue