diff options
| author | Federico Igne <git@federicoigne.com> | 2020-12-30 09:17:15 +0000 |
|---|---|---|
| committer | Federico Igne <git@federicoigne.com> | 2021-11-03 18:55:08 +0000 |
| commit | 63122ddc672e2d287b9cc0e7752675cb0f70f7cf (patch) | |
| tree | 3c399ef05481971df4e33337c0851cb4cccf6938 /rust/rna-transcription/src | |
| parent | bfe7a2e6d7e51b3973a169e592c7f507a7d96c2c (diff) | |
| download | exercism-63122ddc672e2d287b9cc0e7752675cb0f70f7cf.tar.gz exercism-63122ddc672e2d287b9cc0e7752675cb0f70f7cf.zip | |
[rust] RNA Transcription
Diffstat (limited to 'rust/rna-transcription/src')
| -rw-r--r-- | rust/rna-transcription/src/lib.rs | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/rust/rna-transcription/src/lib.rs b/rust/rna-transcription/src/lib.rs new file mode 100644 index 0000000..425f406 --- /dev/null +++ b/rust/rna-transcription/src/lib.rs | |||
| @@ -0,0 +1,38 @@ | |||
| 1 | #[derive(Debug, PartialEq)] | ||
| 2 | pub struct Dna(String); | ||
| 3 | |||
| 4 | #[derive(Debug, PartialEq)] | ||
| 5 | pub struct Rna(String); | ||
| 6 | |||
| 7 | impl Dna { | ||
| 8 | pub fn new(dna: &str) -> Result<Dna, usize> { | ||
| 9 | dna.chars() | ||
| 10 | .enumerate() | ||
| 11 | .find(|(_, n)| !"ACGT".contains(*n)) | ||
| 12 | .map_or_else(|| Ok(Dna(dna.to_string())), |(i, _)| Err(i)) | ||
| 13 | } | ||
| 14 | |||
| 15 | pub fn into_rna(self) -> Rna { | ||
| 16 | Rna(self | ||
| 17 | .0 | ||
| 18 | .as_str() | ||
| 19 | .chars() | ||
| 20 | .map(|n| match n { | ||
| 21 | 'A' => 'U', | ||
| 22 | 'C' => 'G', | ||
| 23 | 'G' => 'C', | ||
| 24 | 'T' => 'A', | ||
| 25 | a => a, | ||
| 26 | }) | ||
| 27 | .collect()) | ||
| 28 | } | ||
| 29 | } | ||
| 30 | |||
| 31 | impl Rna { | ||
| 32 | pub fn new(rna: &str) -> Result<Rna, usize> { | ||
| 33 | rna.chars() | ||
| 34 | .enumerate() | ||
| 35 | .find(|(_, n)| !"ACGU".contains(*n)) | ||
| 36 | .map_or_else(|| Ok(Rna(rna.to_string())), |(i, _)| Err(i)) | ||
| 37 | } | ||
| 38 | } | ||
