Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rezamt
Last active July 31, 2019 00:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rezamt/e8c36bf27ae21154b3288cedec8f0c23 to your computer and use it in GitHub Desktop.
Save rezamt/e8c36bf27ae21154b3288cedec8f0c23 to your computer and use it in GitHub Desktop.
Rust Reference and Ownership Challenges
#[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);
}
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();
}
}
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