use std::collections::HashMap;
use p_rust::customer::eat_at_restaurant;
use crate::garden::vegetables::{Asparagus, AsparagusType};
mod garden;
fn first_word(s: &String) -> usize {
let bytes = s.as_bytes();
for (i, &item) in bytes.iter().enumerate() {
if item == b' ' {
return i;
}
}
s.len()
}
fn first_word2(s: &str) -> &str {
let bytes = s.as_bytes();
for (i, &item) in bytes.iter().enumerate() {
if item == b' ' {
return &s[0..i];
}
}
&s[..]
}
fn no_dangle() -> String {
let s = String::from("hello");
s
}
fn another_function(x: i32) {
println!("The value of x is: {}", x);
}
fn print_label_measurement(value: i32, unit_label: char) {
println!("The measurement is: {}{}", value, unit_label);
}
fn five() -> i32 {
5
}
fn plus_one(x: i8) -> i8 {
x + 1
}
fn takes_ownership(some_string: String) { println!("{}", some_string);
}
fn makes_copy(some_integer: i32) { println!("{}", some_integer);
}
fn gives_ownership() -> String { let some_string = String::from("yours"); some_string }
fn takes_and_gives_back(a_string: String) -> String { a_string }
fn calculate_length(s: String) -> (String, usize) {
let length = s.len(); (s, length)
}
fn calculate_length2(s: &String) -> usize { s.len()
}
fn change_str(some_string: &mut String) {
some_string.push_str(", world");
}
fn main() {
let mut x = 5;
println!("The value of x is: {}", x);
x = 6;
println!("The value of x is: {}", x);
const THREE_HOURS_IN_SECONDS: u32 = 60 * 60 * 3;
println!("THREE_HOURS_IN_SECONDS: {}", THREE_HOURS_IN_SECONDS);
{
let x = x * 2;
println!("The value of x in the inner scope is: {}", x);
}
println!("The value of x is: {}", x);
let spaces = " ";
let spaces = spaces.len();
println!("Len of spaces is: {}", spaces);
let guess: u32 = "42".parse().expect("Not a number!");
println!("guess is: {}", guess);
let y: u8 = 255;
println!("y is {}", y);
let (k, ab) = u8::overflowing_add(254, 3);
println!("k is: {}, ab: {}", k, ab);
let k1 = u8::wrapping_add(254, 3);
println!("k1 is: {}", k1);
let k2 = u8::saturating_add(254, 3);
println!("k2 is: {}", k2);
let k3: u8 = match u8::checked_add(254, 2) {
Some(num) => num,
None => {
println!("k3 has error");
0
}
};
println!("k3 is: {}", k3);
let sum = 5 + 10;
println!("sum: {}", sum);
let difference = 95.5 - 4.3;
println!("difference: {}", difference);
let product = 4 * 30;
println!("product: {}", product);
let quotient: f32 = 56.7 / 32.2;
let floored = 2 / 3; println!("quotient: {}, floored: {}", quotient, floored);
let remainder = 43 % 5;
println!("remainder: {}", remainder);
let t = true;
println!("t: {}", t);
let f: bool = false; println!("f: {}", f);
let c = 'z';
println!("c: {}", c);
let z = 'ℤ';
println!("z: {}", z);
let heart_eyed_cat = '😻';
println!("heart_eyed_cat: {}", heart_eyed_cat);
let tup: (u16, f32, u8) = (500, 6.4, 1);
let (x, y, z) = tup;
println!("x: {}, y: {}, z: {}", x, y, z);
println!("tup: {:?}", tup);
println!("x: {}", tup.0);
let quotient = 56.7 / 32.2;
println!("quotient: {}", quotient);
let floored = 2 / 3;
println!("floored: {}", floored);
let mut arr = [1, 2, 3, 4, 5];
println!("arr[2]: {}", arr[2]);
arr[2] = 6;
println!("mutated arr[2]: {}", arr[2]);
println!("Hello world!");
another_function(5);
print_label_measurement(5, 'h');
let y = {
let x = 3;
x + 1
};
println!("The value of y is: {}", y);
let x = five();
println!("The value of x is: {}", x);
let x = plus_one(5);
println!("The value of x is: {}", x);
let number = 7;
if number < 5 {
println!("condition was true");
} else {
println!("condition was false");
}
if number != 0 {
println!("number was something other than zero");
}
let number = 6;
if number % 4 == 0 {
println!("number is divisible by 4");
} else if number % 3 == 0 {
println!("number is divisible by 3");
} else if number % 2 == 0 {
println!("number is divisible by 2");
} else {
println!("number is not divisible by 4, 3, or 2");
}
let condition = true;
let number = if condition { 5 } else { 6 };
println!("The value of number is: {}", number);
let mut count = 0;
'counting_up: loop {
println!("count = {}", count);
let mut remaining = 10;
loop {
println!("remaining = {}", remaining);
if remaining == 9 {
break;
}
if count == 2 {
break 'counting_up;
}
remaining -= 1;
}
count += 1;
}
println!("End count = {}", count);
let mut counter = 0;
let result = loop {
counter += 1;
if counter == 10 {
break counter * 2;
}
};
println!("The result is {}", result);
let mut number = 3;
while number != 0 {
println!("{}!", number);
number -= 1;
}
println!("LIFTOFF!!!");
let a = [10, 20, 30, 40, 50];
let mut index = 0;
while index < 5 {
println!("the value is: {}", a[index]);
index += 1;
}
for element in a {
println!("the value is: {}", element);
}
for number in (1..4).rev() {
println!("{}!", number);
}
println!("LIFTOFF!!!");
{ let s = "hello"; println!("{}", s);
}
let mut s = String::from("hello");
s.push_str(", world!");
println!("{}", s);
let x = 5;
let y = x;
println!("x: {}, y: {}", x, y);
let s1 = String::from("hello");
let s2 = String::from(&s1);
let s3 = s1.clone();
println!("s1: {}, s2: {}, s3: {}", s1, s2, s3);
{
let s = String::from("hello"); takes_ownership(s);
let x = 5; makes_copy(x); println!("x after makes_copy: {}", x);
}
{
let s1 = gives_ownership(); println!("s1 from gives_ownership: {}", s1);
let s2 = String::from("hello"); let s3 = takes_and_gives_back(s2); println!("s3 from takes_and_gives_back: {}", s3);
}
{
let s1 = String::from("hello");
let (s2, len) = calculate_length(s1);
println!("The length of '{}' is {}.", s2, len);
}
{
let s1 = String::from("hello");
let len = calculate_length2(&s1);
println!("The length of '{}' is {}.", s1, len);
}
{
let mut s = String::from("hello");
change_str(&mut s);
println!("{}", s);
}
{
let mut s = String::from("hello");
let r1 = &mut s;
println!("{}", r1);
}
{
let mut s = String::from("hello");
{
let r1 = &mut s;
println!("r1: {}", r1);
}
let r2 = &mut s;
println!("r2: {}", r2);
}
{
let s = String::from("hello");
let r1 = &s; let r2 = &s;
println!("{} and {}", r1, r2);
}
{
let mut s = String::from("hello");
let r1 = &s; let r2 = &s; println!("{} and {}", r1, r2);
let r3 = &mut s; println!("{}", r3);
}
{
let mut s = no_dangle();
s.push_str(", world");
println!("{}", s);
}
{
let mut s = String::from("hello world");
let word = first_word(&s); s.clear();
println!("{}", word);
}
{
let s = String::from("hello world");
let hello = &s[0..5];
let mut world = String::from(&s[6..11]);
world.push_str("!");
println!("{}, {}", hello, world);
}
{
let s = String::from("hello");
let slice = &s[0..2];
println!("slice 1: {}", slice);
let slice = &s[..2];
println!("slice 2: {}", slice);
}
{
let s = String::from("hello");
let len = s.len();
let slice = &s[3..len];
println!("slice 1: {}", slice);
let slice = &s[3..];
println!("slice 2: {}", slice);
}
{
let s = String::from("hello");
let len = s.len();
let slice = &s[0..len];
println!("slice 1: {}", slice);
let slice = &s[..];
println!("slice 2: {}", slice);
}
{
let s = String::from("hello world"); let word = first_word2(&s);
println!("the first word is: {}", word);
}
{
let mut s = String::from("hello");
s.push_str(", world");
s.push_str("!");
println!("{}", s);
}
{
let my_string = String::from("hello world");
let word = first_word2(&my_string[..6]);
println!("{}", word);
let word = first_word2(&my_string[..]);
println!("{}", word);
let word = first_word2(&my_string);
println!("{}", word);
let my_string_literal = "hello world";
let word = first_word2(&my_string_literal[..6]);
println!("{}", word);
let word = first_word2(&my_string_literal[..]);
println!("{}", word);
let word = first_word2(my_string_literal);
println!("{}", word);
}
{
let a = [1, 2, 3, 4, 5];
let slice = &a[1..3];
assert_eq!(slice, &[2, 3]);
}
{
let vegetable: AsparagusType = garden::test();
println!("vegetable: {}", vegetable);
let plant = Asparagus {};
println!("I'm growing {:?}!", plant);
eat_at_restaurant();
let mut map: HashMap<u8, u8> = HashMap::new();
map.insert(1, 255);
println!("map: {:?}", map);
}
{
let v: Vec<i32> = Vec::new();
println!("v: {:?}", v);
let v = Vec::from([1, 2, 3]);
println!("v: {:?}", v);
let mut v = vec![1, 2, 3, 4];
v.push(5);
v.push(6);
v.push(7);
v.push(8);
let third = &v[2]; println!("The third element is {}", third);
match v.get(100) {
Some(third) => println!("The third element is {}", third),
None => println!("There is no third element")
}
let first = &v[0]; println!("The first element is: {}", first);
for i in &mut v {
*i += 50
}
for i in &v {
println!("{}", i);
}
v.push(9); }
{
#[derive(Debug)]
enum SpreadsheetCell {
Int(i32),
Float(f64),
Text(String),
}
let row = vec![
SpreadsheetCell::Int(3),
SpreadsheetCell::Text(String::from("blue")),
SpreadsheetCell::Float(10.12),
];
println!("row: {:#?}", row);
}
{
let data = "initial contents";
let s = data.to_string();
println!("s: {}", s);
let mut s = "initial contents".to_string();
println!("s: {}", s);
s.push_str("dsdf");
let s = String::from("initial contents");
println!("s: {}", s);
println!("{}", "hello".to_string() + ", world");
println!("{}", format!("hello, {}", "world"));
let mut s = String::from("foo");
s.push_str("bar");
let mut s1 = String::from("foo");
let s2 = "bar";
s1.push_str(s2);
println!("s2 is {}", s2);
let mut s = String::from("lo");
s.push('l');
println!("s: {}", s);
let s1 = String::from("Hello, ");
let s2 = String::from("world!");
let s3 = s1 + &s2; println!("s3: {}", s3);
let s1 = String::from("tic");
let s2 = String::from("tac");
let s3 = String::from("toe");
let s = format!("{}-{}-{}", s1, s2, s3);
println!("s: {}", s);
let hello = String::from("Hola");
println!("hello: {}", hello);
let hello = String::from("Здравствуйте");
println!("hello: {}", hello);
let hello = "Здравству123йте";
let answer = &hello[0..2];
println!("{}", answer);
for c in hello.chars() {
println!("{}", c);
}
for b in hello.bytes() {
println!("{}", b);
}
}
{
let mut scores = HashMap::new();
scores.insert(String::from("Blue"), 10);
scores.insert(String::from("Yellow"), 50);
println!("Blue: {}", scores.get("Blue").unwrap());
println!("Yellow: {}", scores.get("Yellow").unwrap());
let teams = vec![String::from("Blue"), String::from("Yellow")];
let initial_scores = vec![10, 50];
let mut scores: HashMap<_, _> = teams.into_iter().zip(initial_scores.into_iter()).collect();
println!("scores: {:#?}", scores);
let field_name = String::from("Favorite color");
let field_value = String::from("Blue");
let mut map = HashMap::new();
map.insert(&field_name, field_value);
println!("field_name: {}, map: {:?}", field_name, map);
for (key, value) in &scores {
println!("{}: {}", key, value);
}
scores.insert(String::from("Blue"), 10);
scores.insert(String::from("Blue"), 25);
println!("scores: {:#?}", scores);
scores.entry(String::from("Green")).or_insert(55);
scores.entry(String::from("Blue")).or_insert(55);
println!("scores: {:#?}", scores);
let text = "hello world wonderful world";
let mut map = HashMap::new();
for word in text.split_whitespace() {
let count = map.entry(word).or_insert(0);
*count += 1;
}
println!("{:?}", map);
}
}