본문 바로가기

Rust61

[Rust] Iterator 예제 (30일차) fn main() { let v1 = vec![1, 2, 3]; let v1_iter = v1.iter(); for val in v1_iter { println!("got: {}", val); } } #![allow(unused)] fn main() { #[test] fn iterator_demonstration() { let v1 = vec![1, 2, 3]; let mut v1_iter = v1.iter(); assert_eq!(v1_iter.next(), Some(&1)); assert_eq!(v1_iter.next(), Some(&2)); assert_eq!(v1_iter.next(), Some(&3)); assert_eq!(v1_iter.next(), None); } } #[test] fn it.. 2023. 1. 12.
[Rust] Command Line Program 제작 : 환경변수 활용 (29일차) // 지난 lib.rs 코드를 이어서 수정. 아래 명령어를 입력해보자 // $cargo run to poem.txt // $CASE_INSENSITIVE=1 cargo run to poem.txt use std::error::Error; use std::fs::File; use std::io::prelude::*; use std::env; pub struct Config { pub query: String, pub filename: String, pub case_sensitive: bool, } impl Config { pub fn new(args: &[String]) -> Result(query: &str, contents: &'a str) -> Vec(query: &str, contents: &'a .. 2023. 1. 11.
[Rust] Command Line Program 제작 (28일차) // Cargo.toml 과 동일한 위치의 최상단 경로에 poem.txt 생성 I'm nobody! Who are you? Are you nobody, too? Then there's a pair of us — don't tell! They'd banish us, you know. How dreary to be somebody! How public, like a frog To tell your name the livelong day To an admiring bog! // $cargo run the poem.txt 명령어를 입력하여 실행 use std::env; use std::fs::File; use std::io::prelude::*; fn main() { let args: Vec = env::arg.. 2023. 1. 10.
[Rust] Testing 예제_2 (27일차) fn prints_and_returns_10(a: i32) -> i32 { println!("I got the vaue {}", a); 10 } #[cfg(test)] mod tests { use super::*; #[test] fn this_test_will_pass() { let value = prints_and_returns_10(4); assert_eq!(10, value); } #[test] fn this_test_will_fail() { let value = prints_and_returns_10(8); assert_eq!(5, value); } } pub fn add_two(a: i32) -> i32 { a + 2 } #[cfg(test)] mod tests { use super::*; #[.. 2023. 1. 9.