Rust lr1 code

parent 10711ec5
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "lr1"
version = "0.1.0"
[package]
name = "lr1"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
use std::io;
fn main()
{
println!("Enter your name:");
let mut name = String::new();
io::stdin()
.read_line(&mut name)
.expect("Failded to read line");
println!("Hello, {}!", name);
}
fn main()
{
let mut x : u32 = 3;
println!("{}", x);
x +=x;
println!("{}", x);
}
fn main()
{
let s1 = "Hello world!";
let s2 = "Goodbye world!";
let s3 = "rust";
println!("len of {} = {}", s1, lenght(s1));
println!("len of {} = {}", s2, lenght(s2));
println!("len of {} = {}", s3, lenght(s3));
}
fn lenght(s: &str) -> usize
{
let mut length = 0;
for _ in s.chars() {
length += 1;
}
length
}
struct Car { brand:String, model:String, year:u16}
fn main()
{
let car1 = Car{
model: String::from("Model S"),
brand: String::from("Tesla "),
year: 2020
};
let car2 = Car{
model: String::from("Model 3"),
brand: String::from("Tesla"),
year: 2017
};
let car3 = Car{
model: String::from("Model X"),
brand: String::from("Tesla"),
year: 2015
};
print_car_info(car1);
print_car_info(car2);
print_car_info(car3);
}
fn print_car_info(car:Car)
{
println!("{} {} made in {}.", car.brand, car.model, car.year);
}
fn main(){
println!("{}", fib(1, 0, 0, 5));
}
fn fib(curr_n:u64, prev_n:u64, recur:u64, end:u64) -> u64{
if recur == end
{
return curr_n;
}
fib(curr_n+prev_n, curr_n, recur+1, end)
}
enum DayOfWeek {
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday,
}
fn main()
{
let today:DayOfWeek = DayOfWeek::Monday;
let tomorrow:DayOfWeek = get_next_day(today);
}
fn get_next_day(curr_day:DayOfWeek) -> DayOfWeek
{
match curr_day{
DayOfWeek::Monday => return DayOfWeek::Thursday,
DayOfWeek::Tuesday=> return DayOfWeek::Wednesday,
DayOfWeek::Wednesday=> return DayOfWeek::Thursday,
DayOfWeek::Thursday=> return DayOfWeek::Friday,
DayOfWeek::Friday=> return DayOfWeek::Saturday,
DayOfWeek::Saturday=> return DayOfWeek::Sunday,
DayOfWeek::Sunday=> return DayOfWeek::Monday,
};
}
#[derive(PartialEq)]
enum Category{
Cpu,
Gpu,
Psu,
Case,
Ram,
Storage,
Generic
}
impl Category{
fn get_string_category(&self) -> &str
{
match self{
Category::Cpu => return "cpu",
Category::Gpu => return "gpu",
Category::Psu => return "psu",
Category::Case => return "case",
Category::Ram => return "ram",
Category::Storage => return "storage",
Category::Generic=> return "unknown",
}
}
fn total_price(products:&[Product], category:Category) -> f64
{
let mut total_price:f64 = 0.0;
for item in products{
if item.category == category{
total_price+=item.price;
}
}
total_price
}
}
struct Product{
name:String,
price:f64,
category:Category
}
impl Product{
fn print_info(&self)
{
println!("Product: {}\n\tPrice: {}\n\tCategory{}", self.name, self.price, self.category.get_string_category());
}
}
fn main()
{
let products = vec![
Product {
name: String::from("CPU"),
price: 100.0,
category: Category::Cpu,
},
Product {
name: String::from("GPU"),
price: 200.0,
category: Category::Gpu,
},
];
let cpu_price = Category::total_price(&products, Category::Cpu);
println!("Total price for CPU: {}", cpu_price);
let gpu_price = Category::total_price(&products, Category::Gpu);
println!("Total price for GPU: {}", gpu_price);
}
#[derive(PartialEq)]
enum Position{
Manager,
Developer,
Designer,
}
impl Clone for Position{
fn clone(&self) -> Self
{
match self{
Position::Manager => Position::Manager,
Position::Developer => Position::Developer,
Position::Designer => Position::Designer,
}
}
}
impl Position{
fn position_to_string(&self ) -> String
{
match self{
Position::Manager => "manager".to_string(),
Position::Developer => "developer".to_string(),
Position::Designer => "developer".to_string(),
}
}
}
struct Employee{
name:String,
position:Position,
salary:f64
}
impl Employee{
fn clone(&self) -> Self{
Employee {
name: self.name.clone(),
position: self.position.clone(),
salary: self.salary
}
}
}
fn employed_on_position(employees:Vec<Employee>, position:Position) -> Vec<Employee>
{
let mut on_picked_position:Vec<Employee> = Vec::new();
for employee in employees{
if employee.position == position{
on_picked_position.push(employee.clone());
}
}
on_picked_position
}
fn main()
{
let employees = vec![
Employee {
name: String::from("Alice"),
position: Position::Manager,
salary: 1200.0,
},
Employee {
name: String::from("Bob"),
position: Position::Developer,
salary: 1200.0,
},
Employee {
name: String::from("Charlie"),
position: Position::Designer,
salary: 1200.0,
}];
let managers = employed_on_position(employees, Position::Manager);
println!("Managers:");
for employee in managers {
println!("\t{} - {} - {}", employee.name, employee.position.position_to_string(), employee.salary);
}
}
{"rustc_fingerprint":12329289877665479636,"outputs":{"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.77.1 (7cf61ebde 2024-03-27)\nbinary: rustc\ncommit-hash: 7cf61ebde7b22796c69757901dd346d0fe70bd97\ncommit-date: 2024-03-27\nhost: x86_64-unknown-linux-gnu\nrelease: 1.77.1\nLLVM version: 17.0.6\n","stderr":""},"15729799797837862367":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.so\nlib___.so\nlib___.a\nlib___.so\n/opt/rust-bin-1.77.1\noff\npacked\nunpacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"gnu\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"linux\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"unknown\"\nunix\n","stderr":""}},"successes":{}}
\ No newline at end of file
Signature: 8a477f597d28d172789f06886806bc55
# This file is a cache directory tag created by cargo.
# For information about cache directory tags see https://bford.info/cachedir/
4b0a7c2f791fd3dd
\ No newline at end of file
{"rustc":16286356497298320803,"features":"[]","declared_features":"","target":1852314693393845464,"profile":5601947868832436996,"path":1684066648322511884,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/lr1-95f6d16c9ed84c49/dep-bin-lr1"}}],"rustflags":[],"metadata":7797948686568424061,"config":2202906307356721367,"compile_kind":0}
\ No newline at end of file
This file has an mtime of when this was started.
\ No newline at end of file
This file has an mtime of when this was started.
\ No newline at end of file
{"rustc":16286356497298320803,"features":"[]","declared_features":"","target":1852314693393845464,"profile":11983525691607113661,"path":1684066648322511884,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/lr1-ad066989226c26eb/dep-test-bin-lr1"}}],"rustflags":[],"metadata":7797948686568424061,"config":2202906307356721367,"compile_kind":0}
\ No newline at end of file
/home/femman/GIT/GitLab/c-programming-2nd-term/rust/lr1/target/debug/deps/liblr1-95f6d16c9ed84c49.rmeta: src/main.rs
/home/femman/GIT/GitLab/c-programming-2nd-term/rust/lr1/target/debug/deps/lr1-95f6d16c9ed84c49.d: src/main.rs
src/main.rs:
/home/femman/GIT/GitLab/c-programming-2nd-term/rust/lr1/target/debug/deps/liblr1-ad066989226c26eb.rmeta: src/main.rs
/home/femman/GIT/GitLab/c-programming-2nd-term/rust/lr1/target/debug/deps/lr1-ad066989226c26eb.d: src/main.rs
src/main.rs:
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment