Rust Sample - String

mohadang·2023년 7월 30일
0

Rust Sample

목록 보기
9/16
post-thumbnail

split

use std::io::{self, Write, Read};

fn main() {
    let stdout = io::stdout();
    let mut out = io::BufWriter::new(stdout.lock());
    let mut input = String::new();
    if let Ok(_) = io::stdin().read_to_string(&mut input) {
        input.split('\n').for_each(|x| {
            let s = x
                .split_ascii_whitespace()
                .filter_map(|y| y.parse::<u32>().ok())
                .sum::<u32>();
            if s != 0 {
                writeln!(out, "{s}", ).unwrap()
            }
        });
    }
}

split_whitespace

use std::io;
fn main() {
    let mut inputs = String::new();
    if let Ok(_) = io::stdin().read_line(&mut inputs) {
        let mut whi = inputs.split_whitespace();
        let a_val = whi.next().unwrap().parse::<i32>().unwrap();
        let b_val = whi.next().unwrap().parse::<i32>().unwrap();
        println!("{}", a_val * b_val);
    }
}

split_ascii_whitespace

use std::io;

fn main() {
    let mut input = String::new();
    if let Ok(_) = io::stdin().read_line(&mut input) {
        let mut sp = input.split_ascii_whitespace();
        if let (Some(n), Some(m)) = (sp.next(), sp.next()) {
            if let (Ok(n), Ok(m)) = (n.parse::<usize>(), m.parse::<u32>()) {
                let mut arr: Vec<u32> = vec![0; n];
                (0..n).for_each(|x| arr[x] = (x + 1) as u32);
                for _ in 0..m {
                    input.clear();
                    if let Ok(_) = io::stdin().read_line(&mut input) {
                        let mut sp = input.split_ascii_whitespace();
                        if let (Some(i), Some(j)) = (sp.next(), sp.next()) {
                            if let (Ok(i), Ok(j)) = (i.parse::<usize>(), j.parse::<usize>()) {
                                arr.swap(i - 1, j - 1);
                            }
                        }
                    }
                }

                println!("{}", arr.iter().map(ToString::to_string).collect::<Vec<String>>().join(" "));
            }
        }
    }
}

trim

use std::io::{self, Write};

fn main() {
    let stdout = io::stdout();
    let mut out = io::BufWriter::new(stdout.lock());
    let mut input = String::new();
    if let Ok(_) = io::stdin().read_line(&mut input) {
        if let Ok(_) = input.trim().parse::<u32>() {
            input.clear();
            if let Ok(_) = io::stdin().read_line(&mut input) {
                let v = input
                    .trim()
                    .split_ascii_whitespace()
                    .filter_map(|x| x.parse::<i32>().ok())
                    .collect::<Vec<_>>();
                writeln!(
                    out,
                    "{} {}",
                    v.iter().min().unwrap(),
                    v.iter().max().unwrap()
                )
                .unwrap();
            }
        }
    }
}

chars

fn main() {
    let mut input = String::new();
    if let Ok(_) = std::io::stdin().read_line(&mut input) {
        if let Ok(n) = input.trim().parse::<usize>() {
            (0..n).for_each(|_|{
                input.clear();
                if let Ok(_) = std::io::stdin().read_line(&mut input) {
                    let input = input.trim();
                    println!("{}{}", 
                        &input.chars().nth(0).unwrap(), 
                        &input.chars().last().unwrap());
                }
            });
        }
    }
}

nth

fn main() {
    let mut str_input = String::new();
    if let Ok(_) = std::io::stdin().read_line(&mut str_input) {
        let mut num_input = String::new();
        if let Ok(_) = std::io::stdin().read_line(&mut num_input) {
            if let Ok(i) = num_input.trim().parse::<usize>() {
                println!("{}", str_input.chars().nth(i - 1).unwrap());
            }
        }        
    }
}

count

fn main() {
    let mut str_input = String::new();
    if let Ok(_) = std::io::stdin().read_line(&mut str_input) {
        println!("{}", str_input.trim().chars().count());
    }
}

rev

use std::io;

fn main() -> io::Result<()> {
    let mut input = String::new();
    io::stdin().read_line(&mut input)?;
    let mut sp = input.trim().split_ascii_whitespace();
    if let (Some(val1), Some(val2)) = (sp.next(), sp.next()) {
        let val1: String = val1.chars().rev().collect();
        let val2: String = val2.chars().rev().collect();
        if let (Ok(val1), Ok(val2)) = (val1.parse::<usize>(), val2.parse::<usize>()) {
            println!("{}", val1.max(val2));
        }
    }

    Ok(())
}
profile
mohadang

0개의 댓글