Skip to content

Instantly share code, notes, and snippets.

@gusdelact
Created January 25, 2024 08:51
Show Gist options
  • Save gusdelact/bf8c54c693319ba2305d2ecff6f5ef51 to your computer and use it in GitHub Desktop.
Save gusdelact/bf8c54c693319ba2305d2ecff6f5ef51 to your computer and use it in GitHub Desktop.
use std::collections::HashMap;
fn f1(x:i32) -> Option<bool>
{
if x>=0 && x<=100
{
Some(true)
}
else if x>=101 && x<=200
{
Some(false)
}
else
{
None
}
}
fn f2(x:f32) -> Result<f32,u32>
{
if x!=0.0 && x>0.0
{
Ok( 1.0/x)
}
else if x<0.0
{
Err(404)
}
else
{
Err(500)
}
}
fn main()
{
let mut vec01: Vec<i32> = Vec::new();
vec01.push(5);
for i in 101..110
{
vec01.push(i);
}
println!("{:?}",vec01);
println!("{}",vec01[3]);
println!("{}",vec01.len());
let top = &vec01[0];
println!("{:?}",top);
let mut bottom = &vec01[vec01.len()-1];
println!("{:?}",bottom);
vec01.push(130);
println!("{:?}",vec01);
bottom = &vec01[vec01.len()-1];
println!("{:?}",bottom);
vec01.remove(vec01.len()-1);
bottom = &vec01[vec01.len()-1];
println!("{:?}",bottom);
println!("{:?}",f1(1).unwrap());
println!("{:?}",f1(150).unwrap());
println!("{:?}",f1(1500));
println!("{:?}",f2(0.5));
println!("{:?}",f2(0.333).unwrap());
println!("{:?}",f2(0.0));
match f2(-1.0)
{
Ok(x) => println!("Resultado {}",x),
Err(404) => println!("no dar valor 0"),
Err(500) => println!("no dar valor negativo"),
Err(0_u32..=403_u32) | Err(405_u32..=499_u32) | Err(501_u32..=u32::MAX) => panic!("Codigo no conocido")
}
let mut hsh1: HashMap<String,i32>= HashMap::new();
hsh1.insert(String::from("CUTG"),1);
hsh1.insert(String::from("TOMD"),2);
let v0=hsh1.get("CUTG");
match v0
{
Some(v) => println!("{}",v),
None => println!("No existe")
};
let v1=hsh1.get("AAA");
match v1
{
Some(v) => println!("{}",v),
None => println!("No existe")
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment