Created
August 22, 2024 22:26
-
-
Save JonathanLoscalzo/678072b14d9f1db889a18ea237dca42d to your computer and use it in GitHub Desktop.
rust - random things
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
// // strings3.rs | |
// // | |
// // Execute `rustlings hint strings3` or use the `hint` watch subcommand for a | |
// // hint. | |
// // I AM NOT DONE | |
// fn trim_me(input: &str) -> String { | |
// // TODO: Remove whitespace from both ends of a string! | |
// input.trim().to_string() | |
// } | |
// fn compose_me(input: &str) -> String { | |
// // TODO: Add " world!" to the string! There's multiple ways to do this! | |
// (input.push " world!").to_string() | |
// } | |
// fn replace_me(input: &str) -> String { | |
// // TODO: Replace "cars" in the string with "balloons"! | |
// ??? | |
// } | |
// #[cfg(test)] | |
// mod tests { | |
// use super::*; | |
// #[test] | |
// fn trim_a_string() { | |
// assert_eq!(trim_me("Hello! "), "Hello!"); | |
// assert_eq!(trim_me(" What's up!"), "What's up!"); | |
// assert_eq!(trim_me(" Hola! "), "Hola!"); | |
// } | |
// #[test] | |
// fn compose_a_string() { | |
// assert_eq!(compose_me("Hello"), "Hello world!"); | |
// assert_eq!(compose_me("Goodbye"), "Goodbye world!"); | |
// } | |
// #[test] | |
// fn replace_a_string() { | |
// assert_eq!(replace_me("I think cars are cool"), "I think balloons are cool"); | |
// assert_eq!(replace_me("I love to look at cars"), "I love to look at balloons"); | |
// } | |
// } | |
// fn main(){ | |
// let obj = MyStruct { value: &"Hello".to_string()}; | |
// println!("{}", obj.value); | |
// owner_sure(obj.value); | |
// } | |
// struct MyStruct<'a>{ | |
// value: &'a String | |
// } | |
// fn owner_sure(value: &String){ | |
// println!("{}", value) | |
// } | |
// struct Something | |
// enum Message { | |
// // TODO: implement the message variant types based on their usage below‡ | |
// Move(Point), | |
// Echo(String), | |
// ChangeColor(u8, u8, u8), | |
// Quit, | |
// } | |
// struct Point { | |
// x: u8, | |
// y: u8, | |
// } | |
// struct State { | |
// color: (u8, u8, u8), | |
// position: Point, | |
// quit: bool, | |
// message: String, | |
// } | |
// impl State { | |
// fn change_color(&mut self, color: (u8, u8, u8)) { | |
// self.color = color; | |
// } | |
// fn quit(&mut self) { | |
// self.quit = true; | |
// } | |
// fn echo(&mut self, s: String) { | |
// self.message = s | |
// } | |
// fn move_position(&mut self, p: Point) { | |
// self.position = p; | |
// } | |
// fn process(&mut self, message: Message) { | |
// // TODO: create a match expression to process the different message | |
// // variants | |
// // Remember: When passing a tuple as a function argument, you'll need | |
// // extra parentheses: fn function((t, u, p, l, e)) | |
// match message { | |
// Message::ChangeColor(r, g, b) => self.change_color((r, g, b)), | |
// Message::Echo(s) => self.echo(s), | |
// Message::Move(point) => self.move_position(point), | |
// Message::Quit => self.quit(), | |
// _ => (), | |
// } | |
// } | |
// } | |
// #[cfg(test)] | |
// mod tests { | |
// use super::*; | |
// #[test] | |
// fn test_match_message_call() { | |
// let mut state = State { | |
// quit: false, | |
// position: Point { x: 0, y: 0 }, | |
// color: (0, 0, 0), | |
// message: "hello world".to_string(), | |
// }; | |
// state.process(Message::ChangeColor(255, 0, 255)); | |
// assert_eq!(state.color, (255, 0, 255)); | |
// state.process(Message::Echo(String::from("Hello world!"))); | |
// assert_eq!(state.message, "Hello world!"); | |
// state.process(Message::Move(Point { x: 10, y: 15 })); | |
// assert_eq!(state.position.x, 10); | |
// assert_eq!(state.position.y, 15); | |
// state.process(Message::Quit); | |
// assert_eq!(state.quit, true); | |
// } | |
// } | |
// iterators3.rs | |
// | |
// This is a bigger exercise than most of the others! You can do it! Here is | |
// your mission, should you choose to accept it: | |
// 1. Complete the divide function to get the first four tests to pass. | |
// 2. Get the remaining tests to pass by completing the result_with_list and | |
// list_of_results functions. | |
// | |
// Execute `rustlings hint iterators3` or use the `hint` watch subcommand for a | |
// hint. | |
// #[derive(Debug, PartialEq, Eq)] | |
// pub enum DivisionError { | |
// NotDivisible(NotDivisibleError), | |
// DivideByZero, | |
// } | |
// #[derive(Debug, PartialEq, Eq)] | |
// pub struct NotDivisibleError { | |
// dividend: i32, | |
// divisor: i32, | |
// } | |
// // Calculate `a` divided by `b` if `a` is evenly divisible by `b`. | |
// // Otherwise, return a suitable error. | |
// pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> { | |
// if b == 0 { | |
// Err(DivisionError::DivideByZero) | |
// } else if a % b != 0 { | |
// Err(DivisionError::NotDivisible(NotDivisibleError { | |
// dividend: a, | |
// divisor: b, | |
// })) | |
// } else { | |
// Ok(a / b) | |
// } | |
// } | |
// // Complete the function and return a value of the correct type so the test | |
// // passes. | |
// // Desired output: Ok([1, 11, 1426, 3]) | |
// fn result_with_list() -> Result<Vec<i32>, DivisionError> { | |
// let numbers = vec![27, 297, 38502, 81]; | |
// let division_results = numbers.into_iter().map(|n| divide(n, 27).unwrap()); | |
// Ok(division_results.collect()) | |
// } | |
// // Complete the function and return a value of the correct type so the test | |
// // passes. | |
// // Desired output: [Ok(1), Ok(11), Ok(1426), Ok(3)] | |
// fn list_of_results() -> Vec<Result<i32, DivisionError>> { | |
// let numbers = vec![27, 297, 38502, 81]; | |
// let division_results = numbers.iter().map(|n| divide(*n, 27)); | |
// println!("{:?}", numbers); | |
// division_results.collect() | |
// } | |
// #[cfg(test)] | |
// mod tests { | |
// use super::*; | |
// #[test] | |
// fn test_success() { | |
// assert_eq!(divide(81, 9), Ok(9)); | |
// } | |
// #[test] | |
// fn test_not_divisible() { | |
// assert_eq!( | |
// divide(81, 6), | |
// Err(DivisionError::NotDivisible(NotDivisibleError { | |
// dividend: 81, | |
// divisor: 6 | |
// })) | |
// ); | |
// } | |
// #[test] | |
// fn test_divide_by_0() { | |
// assert_eq!(divide(81, 0), Err(DivisionError::DivideByZero)); | |
// } | |
// #[test] | |
// fn test_divide_0_by_something() { | |
// assert_eq!(divide(0, 81), Ok(0)); | |
// } | |
// #[test] | |
// fn test_result_with_list() { | |
// assert_eq!(format!("{:?}", result_with_list()), "Ok([1, 11, 1426, 3])"); | |
// } | |
// #[test] | |
// fn test_list_of_results() { | |
// assert_eq!( | |
// format!("{:?}", list_of_results()), | |
// "[Ok(1), Ok(11), Ok(1426), Ok(3)]" | |
// ); | |
// } | |
// } | |
use std::borrow::Borrow; | |
fn main() { | |
let mut vec = vec![Item { | |
value: "HELLO".to_string(), | |
}]; | |
let mut vec2 = vec![Item { | |
value: "HELLO".to_string(), | |
}]; | |
// let item = vec.get_mut(0); | |
vec.push(Item { | |
value: "WORLD".to_string(), | |
}); | |
// let item = vec.get_mut(0); | |
vec2.push(Item { | |
value: "WORLD".to_string(), | |
}); | |
let s = "Hello".to_string(); | |
check(s); | |
let s = "Hello"; | |
check(s); | |
println!("{:?} {:?}", vec, vec2); | |
// let mut todos = Todos::new(); | |
// todos.add(Item {value: "Hello".to_string()}); | |
} | |
#[derive(Debug)] | |
struct Item { | |
value: String, | |
} | |
use std::borrow::Borrow; | |
fn check<T: Borrow<str>>(s: T) { | |
assert_eq!("Hello", s.borrow()); | |
} | |
// #[derive(Debug)] | |
// struct Todos<T> { | |
// items: Vec<T>, | |
// } | |
// impl<T> Todos<T> { | |
// pub fn new() -> Todos<T> { | |
// Todos { items: vec![] } | |
// } | |
// pub fn add(&mut self, todo: T) { | |
// self.items.push(todo); | |
// } | |
// pub fn get(&mut self, idx: usize) -> &mut T { | |
// self.items.get_mut(idx).unwrap() | |
// } | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment