Currently, mut parameter in fn is just
fn foo(mut self){}
//==
fn foo2(self){
let mut mutself=self
}
I found some situation, that a function may takes owership of a value and then return same type( or itself) back.
Then I get idea to take more use of mut param.
//fn foo(a:mut String,b:String)->i32{
fn foo(mut a:String,b:String)->i32{
a=format!("{}, {}",a,b);
return a.len()
}
let mut a=String::from("a");
let b=String::from("b");
let c=foo(mut a,b);//ownership is given and returned
let d=foo(b,a);//ownership of b is given but not returned
example
/*
pub trait MapperOnce<Input>{
type Output;
fn map(self,value:Input)->Self::Output;
}
pub trait MapperMut<Input>{
type Output;
fn map(&mut self,value:Input)->Self::Output;
}
pub trait Mapper<Input>{
type Output;
fn map(&self,value:Input)->Self::Output;
}*/
pub trait Mapper<Input>{
type Output;
fn map(self,value:Input)->(Self::Output,Self);
}
pub struct AStruct(i32);
impl<'a> Mapper<i32> for &'a AStruct{
type Output=i32;
fn map(self,value:i32)->(i32,Self){
(value+self.0,self)
}
}
pub struct BStruct(i32);
impl<'a> Mapper<i32> for &'a mut BStruct{
type Output=i32;
fn map(self,value:i32)->(i32,Self){
self.0=self.0+1;
(value+self.0,self)
}
}
/*
pub trait Mapper<Input>{
type Output;
fn map(mut self,value:Input)->Self::Output;
}
*/
just to avoid super odd (Self::Output,Self)
mb mut T
is a type?
welp we need abstraction for this situation together with impl ... for &'a mut ...
let mut blaval=3;
fn foo(a:&mut i32){
*a+=1;
}
let dwa=&mut blaval;
foo(dwa);//If we consider &mut i32 is a type, then ownership of dwa should be given
//&mut i32 is not Clone
foo(dwa);//logical, but what makes dwa to be still valid here
foo(dwa);