diff --git a/solutions/rust/rna-transcription/1/Cargo.toml b/solutions/rust/rna-transcription/1/Cargo.toml new file mode 100644 index 0000000..acb68cd --- /dev/null +++ b/solutions/rust/rna-transcription/1/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "rna_transcription" +version = "0.1.0" +edition = "2024" + +# Not all libraries from crates.io are available in Exercism's test runner. +# The full list of available libraries is here: +# https://github.com/exercism/rust-test-runner/blob/main/local-registry/Cargo.toml +[dependencies] diff --git a/solutions/rust/rna-transcription/1/src/lib.rs b/solutions/rust/rna-transcription/1/src/lib.rs new file mode 100644 index 0000000..3aae903 --- /dev/null +++ b/solutions/rust/rna-transcription/1/src/lib.rs @@ -0,0 +1,50 @@ +use std::collections::HashMap; + +#[derive(Debug, PartialEq, Eq)] +pub struct Dna { + coding_strand:String +} + +#[derive(Debug, PartialEq, Eq)] +pub struct Rna { + coding_strand:String +} + +impl Dna { + pub fn new(dna: &str) -> Result { + let nucleotides = ['A', 'T', 'C', 'G']; + let mut seq = String::new(); + for (index, ch) in dna.to_uppercase().chars().enumerate() { + if nucleotides.contains(&ch) { + seq.push(ch); + }else { + return Err(index) + } + } + Ok(Self { coding_strand: seq }) + } + + pub fn into_rna(self) -> Rna { + let pair = HashMap::from([('G','C'),('C','G'), ('T','A'), ('A','U')]); + let mut trans_seq = String::new(); + for ch in self.coding_strand.chars() { + trans_seq.push(pair.get(&ch).unwrap().to_owned()) + } + Rna { coding_strand: trans_seq } + } +} + +impl Rna { + pub fn new(rna: &str) -> Result { + let nucleotides = ['A', 'C', 'G', 'U']; + let mut seq = String::new(); + for (index, ch) in rna.to_uppercase().chars().enumerate() { + if nucleotides.contains(&ch) { + seq.push(ch); + }else { + return Err(index) + } + } + Ok(Self { coding_strand: seq }) + } +}