Last active
July 31, 2019 00:05
-
-
Save rezamt/e8c36bf27ae21154b3288cedec8f0c23 to your computer and use it in GitHub Desktop.
Rust Reference and Ownership Challenges
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[derive(Copy, Clone, Debug)] | |
struct Label<'a> { | |
number: u32, | |
check: u32, | |
name: &'a str | |
} | |
fn main() { | |
let _args: Vec<String> = std::env::args().collect(); | |
let l1 = Label { number: 3 , check: 100_u32, name: "Garry"}; | |
let l2 = l1; | |
let mut l3 = l1; | |
l3.check = 900; | |
l3.name = "Larry Lux"; | |
println!("{:?} {:?} {:?}", l1, l2, l3); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashMap; | |
type Table = HashMap<String, Vec<String>>; | |
fn main() { | |
let _args: Vec<String> = std::env::args().collect(); | |
let mut t1 : Table = Table::new(); | |
t1.insert( String::from("item1"), vec!{"b".to_string(), "a".to_string(),}); | |
t1.insert( String::from("item2"), vec!{"e".to_string(), "e".to_string(),}); | |
t1.insert( String::from("item3"), vec!{"f".to_string(), "h".to_string(),"a".to_string()}); | |
sort_list(&mut t1); | |
for (i, l) in t1 { | |
print!("{} -> ", i); | |
for x in l { | |
print!("{},", x); | |
} | |
println!() | |
} | |
} | |
fn sort_list ( p: &mut Table) { | |
for (_item, list) in p { | |
list.sort(); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let _args: Vec<String> = std::env::args().collect(); | |
let x : &str = "Hello"; | |
let xx : &&str = &x; | |
let xxx : &&&str = &&x; | |
let y : String = x.to_string(); | |
assert!(xx == &"Hello"); | |
assert!(*xx == "Hello"); | |
assert_eq!(xx , &"Hello"); | |
assert_eq!(xxx , &"Hello"); | |
assert_eq!(y , "Hello".to_string()); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment