Algorithm
Dna_to_rna ("Gcat")//=> "Gcau"
The input character T is converted to u, or vice versa.
This question is very easy, but put here, mainly want to hint, rust has two kinds of solution of the pattern.
Non-replace mode:
1,
fn Dna_to_rna (DNA: &str)-> String {
dna.chars (). Map (|c| match c {' T ' => ' U ', K => k}). Collect ()
}
2,
fn Dna_to_rna (DNA: &str)-> String {
dna.split ("T") .collect::<vec<&str>> (). Join ("U")
}
3,
fn Dna_to_rna (DNA: &str)-> String {
dna.chars ()
. Into_iter ()
. Map (|x| match x {
' T ' => return "U". to_string (),
' u ' => return ' T '. to_string (),
_ => return x.to_string (),
})
.collect::< Vec<string>> ()
. Concat ()
}
Replace mode
1,
fn Dna_to_rna (DNA: &str)-> String {
dna.replace ("T", "U")
}
2,
fn Dna_to_rna (DNA: &str)-> String {
str::replace (DNA, "T", "U"). To_string ()
}
3,
fn Dna_to_rna (DNA: &str)-> String {
dna.to_string (). Replace ("T", "U")
}